views:

143

answers:

2

hi All,

I have a regex /(.+)_id$/ in a rails application that matches any string that ends with _id . I need it to match any string that ends with _id except associated_id. How can I accomplish this?

thx :)

-C

+7  A: 
/(.+)(?<!associated)_id$/

will use negative lookbehind to make sure that whatever was matched by (.+) doesn't end in associated.

For languages that don't support lookbehind, you can use this:

/\A(?!.*associated_id$)(.+)_id$/

This will assert that it's not possible to match a string ending in associated_id from the starting position of the string.

Tim Pietzcker
I'm trying to do this in rails, if i plug in "associated_id".match(/(.+)(?<!associated)_id$/) i get SyntaxError: compile error(irb):4: undefined (?...) sequence: /(.+)(?<!associated)_id$/ from (irb):4
Chris Drappier
Oh. Ruby does not support lookbehind, I just found out. Will edit.
Tim Pietzcker
thank you Tim :)
Chris Drappier
A: 

I'm not a "regexpert" so i'm sure there's probably a better way of doing this, but i came up with

/(.+[^associated_id].+)_id$/
Jim McKerchar