Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

regex. how to exclude a substring from the result after it matches the pattern [duplicate]

Tags:

regex

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?

like image 925
ahmedhelmy007 Avatar asked May 16 '11 05:05

ahmedhelmy007


People also ask

What does \+ mean in regex?

Example: The regex "aa\n" tries to match two consecutive "a"s at the end of a line, inclusive the newline character itself. Example: "a\+" matches "a+" and not a series of one or "a"s. ^ the caret is the anchor for the start of the string, or the negation symbol.

How do you find how many occurrences of a regex pattern were replaced in a string?

To count a regex pattern multiple times in a given string, use the method len(re. findall(pattern, string)) that returns the number of matching substrings or len([*re. finditer(pattern, text)]) that unpacks all matching substrings into a list and returns the length of it as well.

How do I match a pattern in regex?

Regular expressions, called regexes for short, are descriptions for a pattern of text. For example, a \d in a regex stands for a digit character — that is, any single numeral 0 to 9. Following regex is used in Python to match a string of three numbers, a hyphen, three more numbers, another hyphen, and four numbers.


2 Answers

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

like image 134
manojlds Avatar answered Sep 22 '22 11:09

manojlds


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

like image 44
daalbert Avatar answered Sep 19 '22 11:09

daalbert