Regular expression - match string containing a but not a and b

Regular expression - match string containing a but not a and b

Post by gfrit » Wed, 07 Feb 2007 22:10:20


Hi,

A quick regex question which I've worked around for the time being,
but would like an answer to if anyone can help.

I want to match on all strings which end _id, but not those ending
row_id.

Some sample strings:

row_id
parent_id
person_id
person

I'd like to match all but the 1st and last.

I've tried many variants, including

preg_match('/(row)\w{0}_id$/',$subject);
preg_match('/(row){0}_id$/',$subject);
preg_match('/(?!row)_id$/',$subject);

None work correctly. As I say I've worked around this issue as I have
very little time, but would like to know the proper solution.

Thanks in advance. Greg.
 
 
 

Regular expression - match string containing a but not a and b

Post by Michael Fe » Wed, 07 Feb 2007 22:46:49

.oO( XXXX@XXXXX.COM )


The last one was very close:

preg_match('/(?<!row)_id$/', $subject);

Notice the '<' to make it a negative look-behind assertion instead of
negative look-ahead.

Micha

 
 
 

Regular expression - match string containing a but not a and b

Post by P Pulkkine » Wed, 07 Feb 2007 23:50:33


< XXXX@XXXXX.COM > kirjoitti
viestiss XXXX@XXXXX.COM ... >> Hi, >> A quick regex question which I've worked around for the time being, >> but would like an answer to if anyone can help. >> I want to match on all strings which end _id, but not those ending >> row_id. >> Some sample strings: >> row_id >> parent_id >> person_id >> person

Somebody answered from the regex point of view, which is not my expertise,
but i ask: do you really need regex to do this, especially if you feel they
are difficult? Can you just applie string finding functions? End is "_id"
but end is not " row_id", that makes two conditions, or do i miss something?
 
 
 

Regular expression - match string containing a but not a and b

Post by gfrit » Sat, 10 Feb 2007 16:59:49

On Feb 6, 2:50 pm, "P Pulkkinen"
<snip>


Hi, sorry for not getting back to the thread earlier. Yes indeed this
could be done with two conditions on a string match.

It was simply the regex challenge! I have a perl project coming up
really soon in which I think I'll be needing to do a lot of regex
work, so just wanted to get some practice in.

Thanks for the replies everyone!
 
 
 

Regular expression - match string containing a but not a and b

Post by gfrit » Sat, 10 Feb 2007 17:19:52


That did the trick, thanks Micha!