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

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

Discussion Overview

The discussion revolves around the differences between two regular expression patterns used in sed: /\.[^.]*$/ and /(\.[^.]*)$/. Participants explore how these patterns behave differently when matching file extensions and the implications of using grouping in regular expressions.

Discussion Character

  • Technical explanation
  • Exploratory

Main Points Raised

  • One participant notes that the first pattern matches file extensions as intended, while the second does not, raising questions about the grouping in the latter.
  • Another participant explains that the behavior of sed can vary based on the version being used, highlighting that older versions require backslash escaping for parentheses to function as grouping characters.
  • It is mentioned that even older versions of sed do not support grouping at all, while modern versions allow for extended regular expressions with the -E option, which changes how parentheses are interpreted.
  • A participant shares a resource they find useful for working with regex, indicating a preference for external tools to test regular expressions.

Areas of Agreement / Disagreement

Participants generally agree on the importance of the sed version in determining how regular expressions function, but there is no consensus on the implications of using grouping in the second pattern.

Contextual Notes

Limitations include the dependency on the specific version of sed being used and the potential for confusion when applying general tutorials to system configuration files.

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
817
Replies
26
Views
6K
  • · 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
2K