regex 正则表达式匹配字符串,但在包含某些元素时不匹配

n53p2ov0  于 2023-10-22  发布在  其他
关注(0)|答案(1)|浏览(74)

我试图用一系列类似的字符串进行正则表达式匹配,但不匹配满足另一个特定标准的其他字符串。我几乎做对了,但在试图排除某些随机长度的名字时遇到了麻烦。
我试图从中间部分解析一个完整的字符串,这里有一些例子。
我想匹配的字符串的例子以及从中提取的内容:

You see a tall athletic dark-brown-haired adult man with dark olive skin and hazel eyes.
You see a short fat yellow-haired youthful man with pale skin and blue eyes.

我想从上面的例子中摘录这些:

tall athletic dark-brown-haired adult man with dark olive skin and hazel eyes
short fat yellow-haired youthful man with pale skin and blue eyes

我不想匹配的字符串的例子,只是忽略:(任何以(你自己)结尾的东西!)或,你知道的人)

You see a masked tall-height heavy man with white skin (yourself!).
You see a furrow-browed older man with cropped pepper-gray hair, whom you know as Richard.
You see a younger man with cropped blue hair, whom you know as Bob.

我对正则表达式的理解有限,这些都在我之上。如果你能帮忙的话,我将不胜感激。
目前为止(部分工作):

^You see (a|an) (.*?)(?<!, whom you know as Steve.|\(yourself!\).)$
iovurdzv

iovurdzv1#

如果不想单独捕获该部分,可以将(a|an)缩短为an?
(.*)感兴趣的部分的捕获组不必是非贪婪的,因为它应该在字符串末尾的点之前结束。
排除使用负先行(?!而不是负后看的行:

^You see an? (?!.*(?:, whom you know as\b|\(yourself!\)))(.*)\.$

Regex demo

相关问题