8

I wonder how i can exclude a substring from the result after it matches the pattern. example:

<a href="?page1"><?php __('string1');?></a>
<a href="?page2"><?php __("string2");?></a>

I want to get only the strings passed as parameters to the __() function. i tried this regex:

'/__\(((\'([^\']+)\')|(\"([^\"]+)\"))/'

but that returns 'string1' and "string2" wrapped in single quotes and double quotations.
how can i exclude single quotes and double quotations?

ahmedhelmy007
  • 191
  • 2
  • 3
  • 10
  • I was talking privately with @ahmedhelmy007 about the problem, we found another solution which is to use the token_get_all() function in php. this is more accurate than regex and faster. – Shreef May 18 '11 at 12:43

4 Answers4

3

You want to try using non-capturing groups - (?:ABC)

manojlds
  • 290,304
  • 63
  • 469
  • 417
2
  • Use (?: ) appropriately. These are for grouping that you don't want to capture.
  • If you have the quotations inside the capture ( ), then the quotes will be included in the capture. If you put the quotes outside, then they will not be included.
  • You have more ( ) than you need. | has the least priority in association.
  • You are escaping more than you need. Quotations don't need to be escapted.
  • Since you are using [^'] and [^"], you don't have to specify for close quotes/parens.

A fix would be like:

'/__\((?:'([^']+)|"([^"]+))/'
sawa
  • 165,429
  • 45
  • 277
  • 381
2

You can use Lookahead and Lookbehind or make the string inside of the quotes a group.

daalbert
  • 1,465
  • 9
  • 7
0

Try this

'/__\(('|")([^\1]+)\1\)/'
       ^1^  ^^2^^^

You can see it online here on Regexr

Every time when you open a round bracket you create a capturing group. So, if you don't want it use (?:) this would define a non capturing group. I don't use this here. I rewrote your regex a bit. In my first group I check if there are ' or " and store them into group 1. later on I use the backreference \1 to this group one, to use the correct character.

Your result is then stored always into group 2. How you access this result depends on your used language.

stema
  • 90,351
  • 20
  • 107
  • 135
  • @stema, your regex is very powerful, specially the backreference, but i couldn't get it to work properly in php !! i use preg_match_all: preg_match_all('/__\((\'|")([^\1]+)\1\)/', $text, $matches); i think that the backreference needs something to work properly inside the range brackets. – ahmedhelmy007 May 17 '11 at 20:12
  • @ahmedhelmy007, I am not a php specialist, but i tried a bit and it seems that you need to escape `'` and `"` so try please this regex. `'/__\((\'|\")([^\1]+)\1\)/'` – stema May 17 '11 at 20:33
  • @stema, no, it is not an escaping problem – ahmedhelmy007 May 18 '11 at 18:55
  • any php geek could tell me what is the problem with the backreference here? in short, i need this to work: preg_match_all('/__\((\'|")([^\1]+)\1/', "__('match this') . 'not this'", $matches); – ahmedhelmy007 May 18 '11 at 19:46
  • @ahmedhelmy007, only a few people will read your comment here. I would suggest that you submit a new question with this problem. Add also the code how you call the regex, and as tag `php`, I am sure you will get very soon good answers. – stema May 18 '11 at 20:09
  • @stema, thanx stema, i have added this question: http://stackoverflow.com/questions/6050427/regex-problem-with-backreference-in-pattern-with-preg-match-all – ahmedhelmy007 May 18 '11 at 21:00