[sed] Extract text captured by a regular expression

Created on November 28, 2026

You can use sed with the -E option to return only the text matched by a capture group in a regular expression. The example below extracts dummy from __dummy__.

echo "__dummy__" | sed -E 's/__(.*)__/\1/'

Explanation

  • The -E option enables extended regular expressions.
  • Use a substitution expression of the form s/regex/replacement/ with -E.
  • \1 represents the text matched by the first capture group. The substitution replaces the entire string with just the text matched by that group.

The command produces the following output.

dummy
Bash Shell command sed