Arun Arun - 1 year ago 49
Bash Question

regular expression for a pattern of 8 characters seperated by a hypen

I wanted a regular expression for a pattern like the following,

1. F639-180C
2. 245A-14F0
3. 319A-15E4
4. A45C-15E5

As I have observed, there will be 8 alphanumeric characters and a hyphen in between. The pattern that I thought was
, I am not sure if this will work fine.

Answer Source

Your regexp was almost correct, if you want to use it with the explicit enumeration of chars, you may do it like this:


or to make it even simpler, it may turn to


where \w{4} match any word character [a-zA-Z0-9_]. I was not sure if you need a global flag - but you can remove it yourself, depending on the task.

Keep in mind, that depending on what regexp you are using, it might turn to an alternate way to match alpanumeric letters [[:alpha:]]{4}-[[:alpha:]]{4}


Improvement: as it was outlined in comments, you probably need to grab only HEX codes, so the regular expression has to take into consideration not the whole set of chars from A to Z, but only HEX codes: [A-Fa-f0-9]

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download