February 19th, 2013, 08:58 AM
Find parenthesis construct
i need a regexp that matches a string within a text from the first opening parenthesis after an equal sign "=(" to its closing parenthesis no matter how many opening and closing parenthesis are between them.
For example :
"The first parenthesis will ( be ignore . an example text containing =(hei() whats(up) with (()yoou) ( what are your plans today)? ) . the next parenthesis will be ignored ) text"
It has to find the following text snippet : =(hei() whats(up) with (()yoou) ( what are your plans today)? )
i cant get this done. Anyone an idea how to solve this? :-/
February 19th, 2013, 09:15 AM
you cannot do this with a regular expression -- unless you have Perl and can make use of the pseudo-regexes also supporting features like recursion.
Regular grammars are the weakest grammars of all. They can only describe a simple sequence of characters, nothing more -- although many people seem to view them as an all-powerful parsing tool.
I guess the best workaround in this case would be to search for "=(" and then simply use a counter to find the closing parenthesis: start the counter at -1, increment it for every ")" and decrement it for every "(". When the counter is 0, you've found the closing parenthesis.
April 1st, 2013, 08:49 AM
not sure what you r trying to ask, but if you want all the text between =("text also containing inner paranthesis." ) then you cawrite regular expression as follows:- =\((.*?)\). you can use backslash to tell interpreter that subsequent character followed in the text should be considered as it is and the the second paranthesis which doesnot have backward slash is you regex to obtain text in the outer paranthesis. and if you want to ignore all the parenthesis then regex is:- =\(([a-z 0-9 A-Z other possible characters excluding parenthesis]+)\)
Last edited by klipangel; April 1st, 2013 at 08:55 AM.
Reason: wanted to make answer more precise
April 1st, 2013, 01:11 PM
The parentheses have to be balanced. What you proposed doesn't support that and would only find "=(hei()" in the example given.
Originally Posted by klipangel
Also, PHP, Perl, and .NET can do this with purely regular expressions.