Why doesn't `\d` work in regular expressions in sed?

RegexLinuxAwkSed

Regex Problem Overview


I am trying to use \d in regex in sed but it doesn't work:

sed -re 's/\d+//g'

But this is working:

sed -re 's/[0-9]+//g'

Regex Solutions


Solution 1 - Regex

\d is a switch not a regular expression macro. If you want to use some predefined "constant" instead of [0-9] expression just try run this code:

s/[[:digit:]]+//g

Solution 2 - Regex

There is no such special character group in sed. You will have to use [0-9].

In GNU sed, \d introduces a decimal character code of one to three digits in the range 0-255. As indicated in this comment.

Solution 3 - Regex

You'd better use the Extended pattern in sed by adding -E. In basic RegExp, \d and some others won't be detected

 -E      Interpret regular expressions as extended (modern) regular expressions rather than basic regular expressions (BRE's).  The re_format(7) manual page fully describes both formats.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser2036880View Question on Stackoverflow
Solution 1 - RegexKamilView Answer on Stackoverflow
Solution 2 - RegexIvaylo StrandjevView Answer on Stackoverflow
Solution 3 - RegexFan YerView Answer on Stackoverflow