Regex help.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
penguinboy
Forum Contributor
Posts: 171
Joined: Thu Nov 07, 2002 11:25 am

Regex help.

Post by penguinboy »

Well, I have a pattern, and it works.
But... I have a feeling there has to be a shorter/better way to write this.

Code: Select all

(
(їA-f0-9])(їA-f0-9])
ї,]
(їA-f0-9])(їA-f0-9])
ї,]
(їA-f0-9])(їA-f0-9])
ї,]
(їA-f0-9])(їA-f0-9])(їA-f0-9])(їA-f0-9])(їA-f0-9])(їA-f0-9])(їA-f0-9])(їA-f0-9])
ї,]
(їA-f0-9])(їA-f0-9])(їA-f0-9])(їA-f0-9])(їA-f0-9])(їA-f0-9])(їA-f0-9])(їA-f0-9])
ї,]
(їA-f0-9])(їA-f0-9])(їA-f0-9])(їA-f0-9])(їA-f0-9])(їA-f0-9])(їA-f0-9])(їA-f0-9])
ї,]
(їA-f0-9])(їA-f0-9])(їA-f0-9])(їA-f0-9])
ї,]
(їA-f0-9])(їA-f0-9])
ї,]
(їA-f0-9])(їA-f0-9])
ї,]
(їA-f0-9])(їA-f0-9])
ї,]
(їA-f0-9])(їA-f0-9])
ї,]
)
some example

1c,cd,90,80a04040,00000377,0000039d,003b,00,01,02,00,
01,01,01,01234567,01234567,01234567,0123,01,01,01,01,
ab,ab,ab,abcdefAB,abcdefAB,abcdefAB,abcd,ab,ab,ab,ab,

Basically the pattern matches
a-f upper or lowercase and 0-9.
x2,x2,x2,x8,x8,x8,x4,x2,x2,x2,x2,


This patteren works and I'm not concerned about execution time,
because I only run this once in the script,
but I would like to understand regex syntax a little more.

I know(hope :) ) there is at least a shorter way to do this....

Anyway.. thanks for any help.

--pb
User avatar
Derfel Cadarn
Forum Contributor
Posts: 193
Joined: Thu Jul 17, 2003 12:02 pm
Location: Berlin, Germany

Post by Derfel Cadarn »

Well, I'm no regex-expert myself, but I know there is this option a{4} which looks for exactly four a's. So, I think you could shorten

Code: Select all

(їA-f0-9])(їA-f0-9])(їA-f0-9])(їA-f0-9])(їA-f0-9])(їA-f0-9])(їA-f0-9])(їA-f0-9])
into

Code: Select all

їA-f0-9]{8}
and so on...

===================================
= Yes, I'm trying to get my 101st posting today =
===================================
penguinboy
Forum Contributor
Posts: 171
Joined: Thu Nov 07, 2002 11:25 am

Post by penguinboy »

Wow thats awsome thanks alot!

I shortened it to:

Code: Select all

(
їA-f0-9]{2}ї,]
їA-f0-9]{2}ї,]
їA-f0-9]{2}ї,]
їA-f0-9]{8}ї,]
їA-f0-9]{8}ї,]
їA-f0-9]{8}ї,]
їA-f0-9]{4}ї,]
їA-f0-9]{2}ї,]
їA-f0-9]{2}ї,]
їA-f0-9]{2}ї,]
їA-f0-9]{2}ї,]
)
Post Reply