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

  • Thread starter sunmaz94
  • Start date
  • Tags
    Grouping
In summary, the conversation discusses the difference between using /\.[^.]*$/ and /(\.[^.]*)$/ in sed regular expressions. It is noted that the former correctly matches file extensions, while the latter may not depending on the version of sed being used. The conversation also mentions the usefulness of a specific website when working with regex.
  • #1
sunmaz94
42
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
  • #2
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).
 
  • #3
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!
 
  • #4
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:))
 
  • #5

What is Regex grouping?

Regex grouping is a way to specify patterns in regular expressions by enclosing them in parentheses. This allows for more precise matching and extraction of specific parts of a string.

How does Regex grouping work?

When a regular expression with grouped patterns is matched against a string, the groups are captured and can be accessed separately. This allows for more flexible and targeted parsing of strings.

What are the benefits of using Regex grouping?

Regex grouping allows for more specific and targeted matching and extraction of patterns in strings. This can be useful in tasks such as data validation, string manipulation, and text parsing.

Are there any limitations to Regex grouping?

Regex grouping can become complex and difficult to read when dealing with multiple nested groups. It also may not be the best solution for all string manipulation tasks, as there are alternative methods available.

How can I learn to use Regex grouping effectively?

Practicing and experimenting with different regular expressions and their groups, as well as studying the documentation and tutorials, can help you become proficient in using Regex grouping effectively. There are also many online resources and tools available to assist with learning and testing regular expressions.

Similar threads

  • Programming and Computer Science
Replies
1
Views
664
  • Programming and Computer Science
Replies
1
Views
626
  • Special and General Relativity
Replies
0
Views
640
  • Programming and Computer Science
Replies
8
Views
2K
  • Linear and Abstract Algebra
Replies
12
Views
3K
  • Classical Physics
Replies
10
Views
950
Replies
1
Views
1K
Replies
8
Views
542
Replies
2
Views
700
Back
Top