Sublime Forum

Regex how to insert object found in replacement

#1

How to replace the
example:

_e("Full Implementation")

find:

_e\("(?:^\\"]+|\\.)*"\)

replace with:
_e("$1", “my_theme”)

As is, the $1 is just being replaced with nothing, but I would like it to be the content between the quotes

0 Likes

#2

You are not capturing a group-1. Try this

_e\("(^\\"]+|\\..*)"\)

but I’m not sure what you are trying to achieve with the backslashes, dots, etc.

0 Likes

#3

I don’t know if it meets your requirement, but this regex will find the contents inside the brackets:

_e\("(.*?)"\)

replace with:

_e\("$1", "my_theme"\)

Leads to:

_e("Full Implementation", "my_theme")
0 Likes

#4

thanks both! I just copied this from somewhere as an example of grabbing a text string.

What needs to be done exactly to ‘capture’ a group?

0 Likes

#5

You have to surround them by round brackets.
E.g.:

(.*?)\.(.*)

Two capture groups.

Let’s say you have a Windows file named “file.xml”

Replacing by $1$2 would lead to:
“filexml”

0 Likes

#6

To clarify, using the ?: after the ( tells the parser that you want this set of parentheses to be non-capturing. Parentheses capture the matched text by default.

0 Likes