Arun Arun - 5 months ago 7
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
"[A-Za-z]|[0-9]{4}"-"[A-Za-z]|[0-9]{4}"
, I am not sure if this will work fine.

Answer

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

/[A-Za-z0-9]{4}\-[A-Za-z0-9]{4}/g

or to make it even simpler, it may turn to

/\w{4}\-\w{4}/g

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]