Does order in sed's regex character classes (e.g. [abc]) matter?

Tom

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.

Kusalananda

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

What does the # character mean in: /bin/sed -e 's#abc#zzz#g'?

From Dev

Does the order matter when declaring classes in Java?

From Dev

Why does the order of alternatives matter in regex?

From Dev

why does order matter in this escaped characters class in sed?

From Dev

does the order when defining functions in classes in python matter

From Dev

Does SCSS (SASS) selector order matter for nested classes

From Dev

split string if next chracter is specific character e.g 'abc'

From Java

Does annotations order matter?

From Dev

Does order of conditions in $and matter?

From Dev

In Java's ForkJoinTask, does the order of fork/join matter?

From Dev

Why does loop order matter when there's strided prefetching?

From Dev

Why does loop order matter when there's strided prefetching?

From Dev

Change regex's result order after sed as order of ls -v

From Dev

SQL - does order of OR conditions matter?

From Java

Does the order of members in a struct matter?

From Dev

Does the order of natural joins matter

From Dev

Does the order of subscribeOn and observeOn matter?

From Dev

Does Python import order matter

From Dev

Does variable order matter for sscanf?

From Dev

Does the order of a Java class matter?

From Dev

Prolog Does the order of recursion matter?

From Dev

Does parameter order matter with tar?

From Dev

Prolog Does the order of recursion matter?

From Dev

xargs: Does order of options matter?

From Dev

Does the order of rules in ABNF matter?

From Dev

.zshrc configuration, does order matter?

From Dev

Does the order of header properties matter?

From Dev

Does the order of constraints in a pred matter?

From Dev

Static regex object or does it matter?

Related Related

HotTag

Archive