![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
Join Date: Sep 2005
Location: GA
Posts: 99
Rep Power: 4
![]() |
I need help matching the follow string with regexp
if(something)
if(something)
do_something
end
endI want the regexp to match nested ifs not matter what how many levels it is and also match the outer if. Here is the code i use to match, but it is not matching correctly (if)(?(if)([\w\W]*?)|([\w\W]*?)\n([\w\W]*?))(end) btw, i am not master in regexp. Thanks |
|
|
|
|
|
#2 |
|
Troll
Join Date: Apr 2005
Location: Texas
Posts: 732
Rep Power: 4
![]() |
I'd reccomend Expresso
__________________
MD5(sig) = bcef75433db02e9ad9bf81d6f7c5c270 |
|
|
|
|
|
#3 |
|
SEXY SHOELESS GOD OF WAR!
![]() Join Date: Jun 2005
Location: Wet west coast of Canada
Posts: 1,197
Rep Power: 5
![]() |
You can use a stack data structure. Imagine you have opening tokens ("if") and closing tokens ("end"), as well as miscellaneous tokens (operators, function calls, etc). You then iterate through the stream of tokens, and every time you come upon an opening token, you push it onto the stack. When you encounter a closing token, you peek at the top of the stack, and if there is a corresponding opening token there, you pop it off. If there is not a matching token, you know there is a syntax error (unmatched closing token). Likewise, if you have parsed all tokens, and the stack is not empty, you have an error (unmatched opening token).
This approach lets you define more than one set of tokens. For example, you can use it to match braces, parentheses, and brackets by defining "{", "(", "[", "}", ")", and "]" as opening and closing tokens.
__________________
And once again, Probability proves itself willing to sneak into a back alley and service Drama as would a copper-piece harlot. - Vaarsuvius, Order of the Stick |
|
|
|
|
|
#4 |
|
Programmer
Join Date: Sep 2005
Location: GA
Posts: 99
Rep Power: 4
![]() |
Thanks for the suggestions
|
|
|
|
|
|
#5 |
|
Expert Programmer
|
http://www.regexlib.com a great resource
__________________
Clifford Matthew Roche <geek@cliffordroche.com> Web Hosting: http://www.crd-hosting.com Consulting: http://www.crdev-consulting.com |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Date Regular Expression | King | C# | 2 | Apr 30th, 2006 10:41 PM |
| Word Frequency Regular Expression | hoffmandirt | C# | 4 | Feb 27th, 2006 9:21 PM |
| Problem with regular expression? | wingz198 | Perl | 3 | Jan 24th, 2006 6:49 PM |
| Regular Expression | toghian | C++ | 12 | Dec 28th, 2005 11:12 PM |
| Regular Expression HELP -- thanks | paulchwd | JavaScript and Client-Side Browser Scripting | 3 | Oct 11th, 2005 5:40 PM |