Page 1 of 1

Hex regex

Posted: Sat Feb 17, 2007 12:05 am
by nwp
I need a regex pattern to match "2B84-5UD-80T7"
i.e. 4 hexadecimals separated by "-" the length of each part (Separated by -) can be 1, 2, 3, .........................
I've tried the following

Code: Select all

|^\d[A-Z]+-+\d[A-z]+-+\d[A-Z]$|
But it did not work.
would anyone please make one for me ??

EDIT
Small caps alphabets are not allowed also thats only A-Z no a-z

Posted: Sat Feb 17, 2007 2:17 am
by GeertDD

Code: Select all

/^([0-9A-Z]+)-([0-9A-Z]+)-([0-9A-Z]+)$/

Edit:
Hmm, I'd love to simplify this regex using backreferences within the pattern, but PCRE doesn't seem to support this..?

Code: Select all

/^([0-9A-Z]+)-\1-\1$/

Posted: Sat Feb 17, 2007 3:31 am
by nwp
Thanks

Posted: Sat Feb 17, 2007 8:27 am
by feyd
GeertDD wrote:Edit:
Hmm, I'd love to simplify this regex using backreferences within the pattern, but PCRE doesn't seem to support this..?

Code: Select all

/^([0-9A-Z]+)-\1-\1$/
Yes, it does. However it does not use the pattern referenced. It uses the matched text.

Side note: the characters aren't hexadecimal.

Posted: Sat Feb 17, 2007 9:12 am
by GeertDD
Thanks for the clarification, feyd. Now I understand.

Had never really used backreferences within a pattern before and this seemed like a good situation to test them -- not. :wink:

Posted: Sat Feb 17, 2007 9:48 am
by Chris Corbyn
Hmm, that doesn't look like hexadecimal to me.... [0-9A-F] would be hexadecimal.

Posted: Sun Feb 18, 2007 10:17 am
by GeertDD
d11wtq wrote:Hmm, that doesn't look like hexadecimal to me.... [0-9A-F] would be hexadecimal.
Right, but if nwp needs to match "2B84-5UD-80T7", you need more than hexadecimal characters alone. I guess he'll be able to figure it out now. :wink: