Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jul 18th, 2006, 9:50 PM   #1
Master
Programmer
 
Master's Avatar
 
Join Date: Sep 2005
Location: GA
Posts: 99
Rep Power: 4 Master is on a distinguished road
Question Regular Expression Patterns

I need help matching the follow string with regexp

if(something)
   if(something)
      do_something
   end
end

I 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
Master is offline   Reply With Quote
Old Jul 19th, 2006, 1:35 AM   #2
Dameon
Troll
 
Dameon's Avatar
 
Join Date: Apr 2005
Location: Texas
Posts: 732
Rep Power: 4 Dameon is on a distinguished road
I'd reccomend Expresso
__________________
MD5(sig) = bcef75433db02e9ad9bf81d6f7c5c270
Dameon is offline   Reply With Quote
Old Jul 19th, 2006, 3:11 AM   #3
lectricpharaoh
SEXY SHOELESS GOD OF WAR!
 
lectricpharaoh's Avatar
 
Join Date: Jun 2005
Location: Wet west coast of Canada
Posts: 1,197
Rep Power: 5 lectricpharaoh will become famous soon enough
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
lectricpharaoh is online now   Reply With Quote
Old Jul 19th, 2006, 12:12 PM   #4
Master
Programmer
 
Master's Avatar
 
Join Date: Sep 2005
Location: GA
Posts: 99
Rep Power: 4 Master is on a distinguished road
Thanks for the suggestions
Master is offline   Reply With Quote
Old Jul 29th, 2006, 11:51 AM   #5
kurifu
Expert Programmer
 
kurifu's Avatar
 
Join Date: Jul 2004
Location: Halifax, Nova Scotia (Canada)
Posts: 784
Rep Power: 5 kurifu is on a distinguished road
Send a message via ICQ to kurifu Send a message via MSN to kurifu
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
kurifu is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 9:16 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC