groups
sed -n 's/abc/xyz/p' re.txt
Output:
xyz 123
Capture
echo 'abc-123' | grep -oE '([a-z]+)-([0-9]+)'
Output:
abc-123
Parentheses create a group
In sed, \1 and \2 refer to captured groups
In grep -o, the whole match is printed
Non capturing
(?:...) groups without capturing
Use it when you need grouping for precedence but do not need the capture
It keeps backreference numbering clean
Backreferences
grep -E '(foo)\1' file.txt
Matches the same text twice Useful for duplicate line detection Not available in all engines
Next
Lookarounds in 06 lookarounds
Posted