Alfred Zhong
Alfred Zhong

Reputation: 7081

In sed, how to represent "alphanumeric or _ or -"

Tried

[a-zA-Z0-9-_]
[a-zA-Z0-0\-\_]
[[[:alnum:]]-_]
...

What is the correct way to represent that in regular expression?

It seems like [a-zA-Z0-9-] works for alphanumeric or dash. But when I add another underscore, it breaks.

Upvotes: 20

Views: 21570

Answers (2)

lurker
lurker

Reputation: 58244

All of these variations will work:

[a-zA-Z0-9_\-]
[a-zA-Z0-9_-]
[-_a-zA-Z0-9]
[-a-z_A-Z0-9]
[-a-zA-Z_0-9]
[-a-zA-Z0-9_]
...

The hyphen needs to be on one end. It can be escaped or not if at the end, and must be un-escaped if at the beginning. The underscore can be almost anywhere, as long as it's not in the middle of one of your ranges, and doesn't need to be escaped.

Upvotes: 1

anubhava
anubhava

Reputation: 785098

That will be this character class:

[[:alnum:]_-]

Which means allow one of these:

1. Alpha numeric
1. Underscore
1. Hyphen

It is important to keep hyphen at 1st or last position in character class to avoid escaping.

Upvotes: 27

Related Questions