What is the difference between /\.[^.]*$/ and /(\.[^.]*)$/?

  • Thread starter Thread starter sunmaz94
  • Start date Start date
  • Tags Tags
    Grouping
Click For Summary
SUMMARY

The discussion clarifies the differences between the regular expressions /\.[^.]*$/ and /(\.[^.]*)$/ in the context of using sed. The former correctly matches file extensions, while the latter does not due to grouping issues. Users must consider the version of sed being used; older versions require backslash escaping for parentheses to function as grouping characters, while modern versions support extended regular expressions with the -E option. Understanding these nuances is crucial for effective regex usage in sed.

PREREQUISITES
  • Familiarity with sed command-line tool
  • Understanding of regular expressions (regex)
  • Knowledge of basic and extended regular expressions
  • Experience with file extension matching
NEXT STEPS
  • Explore the differences between basic and extended regular expressions in sed
  • Learn how to use the -E option in modern versions of sed
  • Practice regex pattern matching with sed on sample files
  • Visit regex testing tools like http://www.regextester.com/ for hands-on practice
USEFUL FOR

This discussion is beneficial for developers, system administrators, and anyone working with text processing using sed and regular expressions.

sunmaz94
Messages
42
Reaction score
0
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.
 
Technology news on Phys.org
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).
 
D H said:
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).

That clears things up significantly. Thanks!
 
D H said:
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).

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:))
 

Similar threads

Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
689
Replies
26
Views
5K
  • · Replies 12 ·
Replies
12
Views
4K
  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 18 ·
Replies
18
Views
2K
  • · Replies 0 ·
Replies
0
Views
1K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 26 ·
Replies
26
Views
1K
  • · Replies 13 ·
Replies
13
Views
1K