1

To my understanding, * matches zero or more and + matches one or more. So when I did this on macOS:

echo "1" |  sed 's/[0-9]*//g'

The number was deleted.

But if I do this:

echo "1" |  sed 's/[0-9]+//g'

The number will still be there.

But shouldn't [0-9]+ matches "1" as well?

SpencerYD
  • 33
  • 5
  • Possible duplicate of [sed plus sign doesn't work](https://stackoverflow.com/questions/22099623/sed-plus-sign-doesnt-work) – Ryszard Czech Sep 21 '19 at 15:42

2 Answers2

1

+ in sed is considered part of the extended-regular expressions, and so, by default, + is not recognized as a special character. Use the -E flag to enable extended regular expressions like so:

echo "1" |  sed -E 's/[0-9]+//g'
Nick Parsons
  • 45,728
  • 6
  • 46
  • 64
1

This is (probably) about whether the sed command is running in POSIX (strict) mode or whether GNU extensions are enabled.

In POSIX mode, a + in a sed regex is not a meta-character.

In GNU extension mode, a + means "one or more repetitions". GNU extensions are enabled using the -E option.

For more information about sed regexes:

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • 2
    This is not so much GNU vs. non-GNU (MacOS has BSD sed), but rather about BRE (Basic Regular Expressions vs. ERE (Extended Regular Expressions). Both are described in the POSIX specification. The -E option interprets the expression as ERE. – Scrutinizer Sep 21 '19 at 18:18