I guess it's best to start with an example:
> echo "[20-20:10]Something" | sed -r -e 's/^\[[0-9\:\-]+(.*)$/\1/'
]Something
> echo "[20-20:10]Something" | sed -r -e 's/^\[[0-9\-\:]+(.*)$/\1/'
-20:10]Something
The only difference is that I swapped :
and -
characters in character class of regex. So: does the order of characters matter in sed's regex's character classes? I doesn't seem to matter on different regex systems, like https://regex101.com/.
I cannot find anything about this behaviour on Google, but I would like to know more, because I want to be sure to know what my scripts do.
Yes it matter, as [0-9\:\-]
matches any single character from the set of digits, backslash, colon, or dash, while [0-9\-\:]
does not match a dash. In the second expression, the dash signifies a range between the backslash character and the backslash character (backslashes are literal is character classes), and the expression is equivalent to [0-9\:]
(or, for that matter [\0-9:]
).
The dash does not signify a range of characters if it's first (possibly after ^
) or last in a character class.
Also note that sed
deals with POSIX regular expressions, which I don't think the site that you link to explicitly supports (see Why does my regular expression work in X but not in Y?).
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments