By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,051 Members | 1,221 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,051 IT Pros & Developers. It's quick & easy.

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

P: n/a
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.

Feb 6 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
..oO(gf****@gmail.com)
>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);
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
Feb 6 '07 #2

P: n/a

<gf****@gmail.comkirjoitti
viestissä:11**********************@q2g2000cwa.goog legroups.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?

Feb 6 '07 #3

P: n/a
On Feb 6, 2:50 pm, "P Pulkkinen"
<snip>
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?
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!

Feb 9 '07 #4

P: n/a
On Feb 6, 1:46 pm, Michael Fesser <neti...@gmx.dewrote:
.oO(gfr...@gmail.com)
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);

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
That did the trick, thanks Micha!

Feb 9 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.