On macOS, d
is part of a regex feature set called enhanced features - note the distinction in name: enhanced, which is NOT the same as extended.
Instead, enhanced features are a separate dimension from basic vs. extended, which can be activated for both basic and extended regexes. In other words: you can have enhanced basic regexes as well as enhanced extended regexes.
However, it appears that whether enhanced features are available in a given utility is precompiled into it; in other words: a given utility either supports enhanced features or it doesn't - no option can change that.
(Options only allow you to choose between basic and extended, such as -E
for sed
and grep
.)
For a description of all enhanced features, see section ENHANCED FEATURES
in man re_format
.
It should also be noted that if POSIX compatibility is important, enhanced features should be avoided with sed
.
There are POSIX utilities, such as awk
, that do support EREs (extended regular expressions), but (a), the POSIX spec explicitly has to state so, and (b) the syntax is limited to POSIX EREs, which are less powerful than the EREs offered by specific platforms.
In practice:
Sadly, the man
pages for the various utilities do NOT state whether a given utility supports enhanced regex features, so it comes down to trial and error.
As of macOS 10.15:
macOS sed
does NOT support enhanced features, which explains the OP's experience.
- E.g.,
sed -E 's/d//g' <<<'a10'
has no effect, because d
isn't recognized as representing a digit (only [[:digit:]]
is).
I have found only one utility that supports enhanced features: grep
:
grep -o 'd+' <<<'a10' # -> '10' - enhanced basic regex
grep -E -o 'd+' <<<'a10' # -> '10' - enhanced extended regex
If you know of others that do, please let us know.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…