Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Regex Grouping Question

  1. Mar 25, 2012 #1
    I was just playing around with sed regular expressions and found something I wouldn't have expected.

    What is the difference between /\.[^.]*$/ and /(\.[^.]*)$/?
    Does the latter group it incorrectly somehow.
    Note that the former does what I want it to, to match an extension of a file, whilst the latter does not.

    Thanks in advance.
     
  2. jcsd
  3. Mar 25, 2012 #2

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    It depends on how you use sed, and which sed you use. Older versions of sed use basic regular expressions. You need to backslash escape the parentheses to make them grouping characters. Even older versions of sed don't even have groups. Modern versions of sed allow extended regular expressions, enabled via the -E option to sed. This makes parentheses are grouping characters (and need to be backslash-escaped to make them ordinary characters).
     
  4. Mar 25, 2012 #3
    That clears things up significantly. Thanks!
     
  5. Mar 25, 2012 #4
    Incidentally, this is one of the reasons one shouldn't play around with sed on system configuration files using only a very general tutorial on how sed is supposed to work. :redface:

    (Hey, I was 13 then! o:))
     
  6. Mar 30, 2012 #5
    Any time I'm working with regex I find this site very useful:
    http://www.regextester.com/
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook