开发者

Match x, but not y anwywhere (not before, between or after the matching pattern)

开发者 https://www.devze.com 2023-01-28 12:51 出处:网络
I want to match several ways of writing a lastname, but that does not match firstname.How can this be done?

I want to match several ways of writing a lastname, but that does not match firstname. How can this be done? I have already the regex below, but I can't find an "AND do NOT match 'first' anywhere" (so neither logical AND or a logical NOT).

(?:(?:(?:last)*.*name)|(?:name.*last)|(?:(?:achter)*.*naam)|(?:familie.*naam)

but since sometimes "name" also means lastname, it is a match for firstname which I don't want. I tried adding, but failed with, expressions like this: [^(?:first)]+, (?!(?:first)), etc.

P.S. I don't have the li开发者_如何学Gobarty of using javascript functions or anything. I have to put this regex into a field of a form of my firefox autofill-addon. So, I need to solve this within the regex itself.


To match anything that does not contain first and is not empty:

^(?:(?!first).)+$

If it has to contain name:

^(?:(?!first).)*name(?:(?!first).)*$

You could also use two checks:

(str.search(/name/i) >=0 && str.search(/first/i) == -1)


I don't think this is possible with pure regexp since regular expressions are exactly those recognizable by a finite state machine.

0

精彩评论

暂无评论...
验证码 换一张
取 消