简体   繁体   中英

Match string that ends with a given substring, except for certain cases, in Rails

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

/(.+)(?<!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.

我不是“正则专家”,所以我确定可能有更好的方法,但是我想出了

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM