我正在尝试使用一系列相似的字符串进行正则表达式匹配,但不匹配满足另一个特定条件的其他字符串。我几乎做对了,但在尝试排除某些随机长度的名称单词时遇到了麻烦。
我正在尝试从中间部分解析完整的字符串,这里有一些例子。
我想要匹配的字符串示例以及从中提取的内容:
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
我不想匹配并忽略的字符串示例:(任何以 (yourself!) 或 结尾的内容,您知道为 )
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!\).)$
如果您不想单独捕获该部分,可以将
(a|an)
缩短为 an?
。
您感兴趣的部分的捕获组不必是非贪婪的,因为它应该在字符串末尾以点结尾。
使用负前瞻排除行
(?!
,而不是负后视:
^You see an? (?!.*(?:, whom you know as\b|\(yourself!\)))(.*)\.$