Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 525
Alfred Zhong Avatar asked Apr 16 '14 21:04

Alfred Zhong


2 Answers

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.

like image 187
anubhava Avatar answered Sep 20 '22 00:09

anubhava


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.

like image 36
lurker Avatar answered Sep 20 '22 00:09

lurker