Anyone know Perl? Need a bit of help with reg exprs

This is a discussion on Anyone know Perl? Need a bit of help with reg exprs within the A Brief History of Cprogramming.com forums, part of the Community Boards category; can anyone tell me why second match operator doesnt find a match when the only difference between the two strings ...

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    552

    Anyone know Perl? Need a bit of help with reg exprs

    can anyone tell me why second match operator doesnt find a match when the only difference between the two strings is the open brace at the end.
    Code:
    $_ = "ax  bx     : cx     dx {";
    $a = "ax";
    $b = "(?:_)?\\w";
    $c = "cx";
    $d = "dx";
    $str1 = "^$a\\s+(b)(?:\\s+:\\s+$c\\s+$b)?\\s*";
    $str2 = "^$a\\s+(b)(?:\\s+:\\s+$c\\s+$b)?\\s*{";
    print "1.".m/$str1/."\n";
    print "2.".m/$str2/."\n";
    print "\n";
    C Code. C Code Run. Run Code Run... Please!

    "Love is like a blackhole, you fall into it... then you get ripped apart"

  2. #2
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    >>$str1 = "^$a\\s+(b)(?:\\s+:\\s+$c\\s+$b)?\\s*";
    This pattern will match the following
    Code:
    ax  bx     : cx     d
    >>$str2 = "^$a\\s+(b)(?:\\s+:\\s+$c\\s+$b)?\\s*{";
    So will this one, see the problem? :-) There's a left over x in the string that the regex doesn't handle, so the match fails. The reason the d is taken but not the x is here
    Code:
    $b = "(?:_)?\\w";
    At the end you match a word character, this is the d :-) There're a bunch of ways to fix it as you'd expect with Perl, here's one
    Code:
    #!usr/bin/perl -w
    
    $_ = "ax  bx     : cx     dx {";
    $a = "ax";
    $b = "(?:_)?";
    $c = "cx";
    $d = "dx";
    
    $str1 = "^$a\\s+(bx)(?:\\s+:\\s+$c\\s+$b$d)?\\s*";
    $str2 = "^$a\\s+(bx)(?:\\s+:\\s+$c\\s+$b$d)?\\s*{";
    
    print "1." . m/$str1/ . "\n";
    print "2." . m/$str2/ . "\n";
    *Cela*

  3. #3
    Registered User
    Join Date
    Jan 2002
    Posts
    552
    Thanks As you can probably tell I'm new at this
    C Code. C Code Run. Run Code Run... Please!

    "Love is like a blackhole, you fall into it... then you get ripped apart"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 16 bit compilar or 32 bit compilar
    By rajkumarmadhani in forum C Programming
    Replies: 16
    Last Post: 12-04-2007, 08:48 AM
  2. Bit processing in C
    By eliomancini in forum C Programming
    Replies: 8
    Last Post: 06-07-2005, 10:54 AM
  3. Porting from 32 bit machine to 64 bit machine!
    By anoopks in forum C Programming
    Replies: 10
    Last Post: 02-25-2005, 07:02 PM
  4. perl need help pls.....
    By magnum38 in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 12-12-2001, 09:35 PM
  5. Array of boolean
    By DMaxJ in forum C++ Programming
    Replies: 11
    Last Post: 10-25-2001, 11:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21