ShowUsYour<Blog>

Irregular expressions regularly

Neat, but probably inefficient example of conditional

Started messing around with conditional matching today and was rather proud of myself when I got this stuff to work.

The pattern matches compound statements in the body of a simple if statement taking into account that there may or may not be braces.

I'll bet a MILLION bucks that there's a *much* more efficient way to do this, but that's for another lesson ;-)


using System ;
using System.Text.RegularExpressions ;

namespace RegexSnippets.Tests
{
    public class Foo
    {
        public static void Main()
        {
          /*
            TRY WITH THIS AS WELL...
            string source = @"if( 1 == 1 ){ 
                            MessageBox.Show( ""this is in the compound statement block."" ) ;
                       } 
                       MessageBox.Show( ""this is not."" ) ;
                    aaa ;" ;
            */
      
            string source = @"if( 1 == 1 ){ 
                            MessageBox.Show( ""this is in the compound statement block."" ) ;
                            MessageBox.Show( ""so is this."" ) ;
                    }
                    aaa ;" ;
            

            string pattern = @"(?'statementType'\w+)\s*?
                        \((?'condition'.*?)\)(?:\s*)?
                            (?'openingCompound'\{)?(?:\s*)?
                            (?(openingCompound)
                                (?'compoundStatements'.*?
                                    (?'closingCompound'})
                                )
                                |
                                (?'compoundStatements'.*?
                                    (?'closingCompound';)
                                )
                            )
                        (?'theRest'.*)" ;

            Regex re = new Regex( 
                    pattern, 
                    RegexOptions.IgnoreCase
|RegexOptions.Multiline
|RegexOptions.IgnorePatternWhitespace
|RegexOptions.Singleline ) ; Match m = re.Match( source ) ;
if( m.Groups["condition"].Success ) { Console.WriteLine("Statement Type: {0}", m.Groups["statementType"]) ; Console.WriteLine("Condition: {0}", m.Groups["condition"]) ; Console.WriteLine("Opening Compound: {0}", m.Groups["openingCompound"].Success) ; Console.WriteLine("Compound Statements: {0}", m.Groups["compoundStatements"].Value.Trim()) ; Console.WriteLine("Closing Compound Char: {0}", m.Groups["closingCompound"].Value.Trim()) ; Console.WriteLine("The rest: {0}", m.Groups["theRest"].Value.Trim()) ; } else { Console.WriteLine("Parser Error!") ; } Console.ReadLine() ; } } }
Posted: May 06 2003, 08:51 PM by digory | with 10 comment(s)
Filed under:

Comments

Lessypearlene said:

buy a <a href=guccimirrorimage.jimdo.com/>gucci mirror image</a>  online   , just clicks away

# December 29, 2011 9:10 PM

DierEcary said:

check this link, <a href=www.e-louisvuittonbags.com/louis-vuitton-handbags-c-54.html>louis vuitton handbags</a>  for more    to get new coupon

# January 4, 2012 9:56 PM

Lessyangel said:

buy best <a href=vob-to-3gp.weebly.com/>vob to 3gp</a>   to get new coupon   online shopping

# January 7, 2012 3:44 PM

Fanknancy said:

cheap   for more <a href=cdac.overcognition.com   for promotion code

# January 16, 2012 6:00 AM

hesegenevive said:

I am sure you will love    and get big save <a href=www.swet.jp/.../a>  suprisely

# January 19, 2012 1:40 PM

WERMCYNC said:

buy best <a href=chanelhandbag2011s.weebly.com/>chanel handbag 2011</a>   to take huge discount

# January 28, 2012 3:56 PM

DierErenna said:

view <a href=rip-dvd-to-apple-tv.weebly.com/>rip dvd to apple tv</a>  for less   with low price

# January 29, 2012 6:09 PM

Mitolors said:

click to view <a href=www.monsterbeatsny.com/>monster beats by dr dre</a>   to get new coupon

# March 14, 2012 7:27 PM

taceElaw said:

buy best <a href=www.buycheapchanelbags2011.com/>chanel 2010 handbags</a>   for more detail

# April 11, 2012 1:03 AM

Siseerex said:

you love this?  <a href=outlet-louisvuitton.wallinside.com/.../a>   to take huge discount

# April 18, 2012 2:55 PM
Leave a Comment

(required) 

(required) 

(optional)

(required)