Home Download Buy Blog Forum Support

Regex how to insert object found in replacement

Regex how to insert object found in replacement

Postby cutcopypaste on Fri Dec 21, 2012 3:45 pm

How to replace the
example:
Code: Select all
_e("Full Implementation")


find:
Code: Select all
_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
cutcopypaste
 
Posts: 41
Joined: Tue Feb 21, 2012 9:43 pm

Re: Regex how to insert object found in replacement

Postby agibsonsw on Fri Dec 21, 2012 3:57 pm

You are not capturing a group-1. Try this

Code: Select all
_e\("([^\\"]+|\\..*)"\)


but I'm not sure what you are trying to achieve with the backslashes, dots, etc.
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
agibsonsw
 
Posts: 901
Joined: Fri Jan 27, 2012 9:11 pm

Re: Regex how to insert object found in replacement

Postby highend on Fri Dec 21, 2012 3:59 pm

I don't know if it meets your requirement, but this regex will find the contents inside the brackets:
Code: Select all
_e\("(.*?)"\)


replace with:
Code: Select all
_e\("$1", "my_theme"\)


Leads to:
Code: Select all
_e("Full Implementation", "my_theme")
highend
 
Posts: 94
Joined: Fri Jan 20, 2012 2:47 pm

Re: Regex how to insert object found in replacement

Postby cutcopypaste on Fri Dec 21, 2012 4:22 pm

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?
cutcopypaste
 
Posts: 41
Joined: Tue Feb 21, 2012 9:43 pm

Re: Regex how to insert object found in replacement

Postby highend on Fri Dec 21, 2012 4:53 pm

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

Code: Select all
(.*?)\.(.*)


Two capture groups.

Let's say you have a Windows file named "file.xml"

Replacing by $1$2 would lead to:
"filexml"
highend
 
Posts: 94
Joined: Fri Jan 20, 2012 2:47 pm

Re: Regex how to insert object found in replacement

Postby adzenith on Fri Dec 21, 2012 5:30 pm

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.
adzenith
 
Posts: 1217
Joined: Mon Oct 19, 2009 9:12 pm


Return to Technical Support

Who is online

Users browsing this forum: Yahoo [Bot] and 22 guests