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

13,209

Solution 1

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.

Solution 2

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.

Share:
13,209
Alfred Zhong
Author by

Alfred Zhong

Updated on July 01, 2022

Comments

  • Alfred Zhong
    Alfred Zhong almost 2 years

    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.

  • Phil Perry
    Phil Perry about 10 years
    To elaborate, if an un escaped hyphen - shows up anywhere but the very first or last position, the regex processor thinks you want a range rather than a lone character.
  • Amitesh Bharti
    Amitesh Bharti over 4 years
    To fetch alphanumeric characters of a file sed '/^[^-[:alnum:]]/d' file
  • soMuchToLearnAndShare
    soMuchToLearnAndShare almost 4 years
    for some reason, the [[:alnum:]_-] somehow match the forward slash too and followed by 1 more letter. meaning, it somehow match things like this: abcdef/somethingelse as abcdef/s
  • anubhava
    anubhava almost 4 years
    No that cannot match a /. That would be because of absence of anchors.