4

I have the following code:

echo "12. Chapter Name" | sed -n -E "s/([0-9]{2})\.[[:space:]].*/\1/p"

It prints 12 as expected, since \1 refers to the first capturing group.

However, if \0 is used instead of \1, the output is 12. Chapter Name, the entire input string is printed.

It seems that as long as the regex found a match, \0 prints the entire input string. Is this correct?

I'm running Debian 10.2.

Ryszard Czech
  • 18,032
  • 4
  • 24
  • 37
Peppershaker
  • 111
  • 1
  • 8

1 Answers1

7

The \0 is a placeholder for the whole match value.

Note that capturing group indices start with 1 and go up until 9 in POSIX regex patterns. The zeroth group is the whole match.

Also, instead of \0, it is more common - and probably more portable - to use & in sed.

echo "12. Chapter Name" | sed -n -E "s/([0-9]{2})\.[[:space:]].*/\0/p"

is equal to

echo "12. Chapter Name" | sed -n -E "s/([0-9]{2})\.[[:space:]].*/&/p"

You may read more about & at Using & as the matched string.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563