Amazon holiday deals See deals →ad

Mets Minor League Blog

Music theory, instruments and musicians

Mets Minor League Blog

Music theory, instruments and musicians

Linux grep Regex Groups: Syntax and Examples

Yes, Linux grep can match regular-expression groups, but the syntax depends on which regex mode you use. With ordinary grep, capture-style parentheses must usually be escaped as \(…\); with grep -E, use plain parentheses, as in grep -E '([0-9]{4})-([0-9]{2})' file.txt.

What a regex group does

Parentheses group parts of a regular expression so operators apply to them together. For example, (ab)+ matches one or more consecutive repetitions of ab. Groups can also mark a portion of a match for later reference in some regex tools.

It helps to separate two ideas that are often both called “groups”:

  • Grouping for matching: Parentheses change how regex components are combined or repeated.
  • Capturing: A program stores the text matched inside parentheses so it can be retrieved or reused.

grep is primarily a line-searching tool: it selects and prints lines that match. It does not provide a general interface for returning each captured group as a separate field. Its support for backreferences varies by regex mode and implementation, so do not assume that a parenthesized group will be displayed by grep.

Basic and extended regex syntax

By default, GNU grep uses basic regular expressions (BRE). In BRE, parentheses are generally ordinary characters unless escaped. To group a pattern, write \( and \):

grep 'ab\(cd\)\+' file.txt

In GNU BRE, the escaped \+ means “one or more” of the preceding expression. BRE details can differ among Unix tools, so this form is not as portable as the corresponding extended expression.

Extended regular expressions (ERE) make parentheses and several other operators easier to read. Enable them with -E:

grep -E 'ab(cd)+' file.txt

This matches lines containing abcd, abcdcd, and so on. The parentheses make cd the unit repeated by +. In ERE, +, ?, and | are also operators without backslashes; in GNU BRE, their usual operator forms are escaped. For portable scripts, choose a mode explicitly and check the target system’s manual.

Practical group examples

Suppose a log contains dates such as 2026-11-04. This command finds lines containing a year, month, and day separated by hyphens:

grep -E '[0-9]{4}-[0-9]{2}-[0-9]{2}' app.log

Here the character classes and repetition counts do the work; parentheses are not necessary unless you want to group parts for another operator. To require either an ERROR or WARN label, use an ERE group with alternation:

grep -E '(ERROR|WARN):' app.log

The parentheses keep the alternatives together, and | means “or.” To match either a repeated pair or a repeated three-character sequence, for example, use grouping around the complete unit:

grep -E '(ab|xyz)+' file.txt

That pattern matches one or more repetitions of either ab or xyz. It does not print just the selected alternative; it prints the matching line, unless an option changes the output format.

Backreferences and getting captured text

A backreference matches text previously matched by a capturing group. GNU grep supports backreferences in its basic and extended regex modes, using forms such as \1 for the first group. For example, this finds a repeated word on a line:

grep -E '\b([[:alpha:]]+)\s+\1\b' file.txt

In GNU grep’s ERE syntax, \1 refers to the text matched by the first parenthesized group. The pattern uses GNU-supported word-boundary and whitespace escapes; those escapes are not universal across all grep implementations. A more portable alternative may require a different expression or tool.

Even when a backreference helps decide whether a line matches, it does not make grep print the captured text alone. The -o option prints each nonempty part of a line that matches the whole regular expression, not a chosen capture group. If you need structured fields or captured values, use a tool designed to extract them, such as sed, awk, or a programming language with regex capture APIs.

Choosing grep options and avoiding pitfalls

  • Use grep -E for readable groups, alternation, and repetition operators such as + and ?.
  • Use plain grep when you need BRE syntax or are following an existing pattern, but remember that grouping parentheses are escaped.
  • Use grep -F when the search string is literal text rather than a regex. In fixed-string mode, parentheses have no grouping role.
  • Quote the pattern in the shell, commonly with single quotes, so the shell does not interpret characters such as parentheses, dollar signs, or asterisks before grep sees them.
  • Check which grep you have if a pattern relies on GNU-specific extensions. GNU, BSD, and other implementations do not necessarily support every escape or extension identically.

A frequent misconception is that grep -o means “print capture group one.” It does not: it prints the entire portion matched by the regex. Another common mistake is mixing modes, for example writing \(…\) while expecting ERE syntax. When a pattern fails, first confirm whether -E is present, then check the parentheses and repetition operators for that mode.

Frequently Asked Questions

Does grep support regex groups?
Yes. Use \(…\) for grouping in basic regex mode, or (…) with grep -E. Support for particular extensions can depend on the grep implementation.

How do I use parentheses with grep?
For extended regex, write a pattern such as grep -E '(cat|dog)' file. For basic regex, write grep '\(cat\|dog\)' file with GNU grep; details of BRE alternation can vary, so -E is usually clearer.

How can I print only a capture group with grep?
There is no general grep option that prints an arbitrary capture group. -o prints the whole regex match. Use a suitable extraction tool, or design the regex so the entire match is exactly the text you want.

What does \1 mean in grep?
Where supported, \1 is a backreference to the text matched by the first group. It can require repeated text to be identical, but it does not output that group separately.

Should I use grep or sed for capture groups?
Use grep to find matching lines. If you need to transform text or extract a particular matched portion, sed, awk, or a language with explicit capture-group results is often a better fit.

Linux grep Regex Groups: Syntax and Examples
Scroll to top