Previous

 


30. Regexes can't do everything

  • Finally, a word of warning - regexes can't do everything.
  • While they are very powerful, and you'll find that they come in handy in all sorts of places and for all sorts of tasks, there are some things that they just can't do.
  • The classic example is trying to match balanced sets of brackets.
  • Consider the code:
      var = functionA(functionB(12) * 7) + functionC(56);
    
  • Now write a regex to match:
      (functionB(12) * 7)
    
    as well as any other depth of balanced parentheses.
  • Possible regexes could be:
      Option 1: \(.*\)
      Option 2: \([^)]*\)
      Option 3: \([^()]*\)
    
    but the resuts would be:
      Option 1: (functionB(12) * 7) + functionC(56)
      Option 2: (functionB(12)
      Option 3: No match
    
  • In fact, you can't write a regex to match any depth of balanced brackets - you can do it for one depth, but it will work for only that depth.
  • (For the example above:
      #!/usr/bin/perl
      $_ = "var = functionA(functionB(12) * 7) + functionC(56);";
    
      if (m/(\([^()]*(\([^()]*\)[^()]*)*\))/) {
        print "$1\n";
      }
    
    is what you want.)

Previous

 

Andrew Hill

For LinuxSA Meeting, 17 April 2001