Regular expression. how to exclude a substring from the result after it matches the pattern

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 returns "string1" and "string2" enclosed in single quotes and double quotes.
how can i exclude single quotes and double quotes?

+3
source share
4 answers

try it

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

You can see it online here at Regexr

, , . , , (?:), , . . . ' " 1. backreference \1 , .

2. , .

+2
  • (?: ) . , .
  • ( ), . , .
  • ( ), . | .
  • , . .
  • [^'] [^"], /.

:

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

Do you want to try using non-exciting bands - (?:ABC)

0
source

You can use Lookahead and Lookbehind or make the string inside quotation marks a group.

0
source

All Articles