Search code examples
regexsed

To match a digit inside a negated set


I need to match an arbitrary digit inside a negated set, but becasue sed doesn't support \d, I'm not sure how to do it.

Works with PCRE, but not with sed:
^[^\d]*\d*

Instead of \d, I can use [0-9] or [[:digit:]], but how to include it inside a negated set?

Doesn't work
^[^[0-9]]*[0-9]*
Doesn't work
^[^[[:digit:]]]*[0-9]*
Works, but ugly. I suppose there are better ways...
^[^0123456789]*[0-9]*

Solution

  • A character class of [[:digit:]] within a negated set can be expressed as [^[:digit:]], so your working regex of ^[^0123456789]*[0-9]* can be revised as:

    ^[^[:digit:]]*[[:digit:]]*