Hex regex

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
nwp
Forum Contributor
Posts: 105
Joined: Sun Feb 04, 2007 12:25 pm

Hex regex

Post 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
User avatar
GeertDD
Forum Contributor
Posts: 274
Joined: Sun Oct 22, 2006 1:47 am
Location: Belgium

Post 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$/
nwp
Forum Contributor
Posts: 105
Joined: Sun Feb 04, 2007 12:25 pm

Post by nwp »

Thanks
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
GeertDD
Forum Contributor
Posts: 274
Joined: Sun Oct 22, 2006 1:47 am
Location: Belgium

Post 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:
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Hmm, that doesn't look like hexadecimal to me.... [0-9A-F] would be hexadecimal.
User avatar
GeertDD
Forum Contributor
Posts: 274
Joined: Sun Oct 22, 2006 1:47 am
Location: Belgium

Post 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:
Post Reply