正则表达式(PCRE)从匹配结果中排除某些单词

Regex (PCRE) exclude certain words from match result

我只需要获取名称为粗体的字符串:

author={Trainor, Sarah F and Calef, Monika and Natcher, David and Chapin, F Stuart and McGuire, A David and Huntington, Orville and Duffy, Paul and Rupp, T Scott and DeWilde, La'Ona and Kwart, Mary and others},

有没有办法跳过匹配结果中的所有 'and' 'others' 个单词?

尝试了很多事情,但没有像我期望的那样有效

(?<=\{).+?(?<=and\s).+(?=\})

您可以利用 \G 和捕获组来匹配您。

这些值在捕获组 1 中。

(?:author={|\G(?!^))([^\s,]+,(?:\h+[^\s,]+)+)\h+and\h+(?=[^{}]*\})

关于图案

  • (?:非捕获组
    • author={字面匹配
    • |
    • \G(?!^) 在上一场比赛结束时声明位置,而不是在开始时
  • )关闭非捕获组
  • ( 捕获 组 1
    • [^\s,]+, 不匹配空白字符或逗号,然后匹配逗号
    • (?:\h+[^\s,]+)+ 重复 1+ 次匹配 1+ 个水平空白字符,然后匹配除空白字符和逗号之外的任何字符
  • ) 关闭组 1
  • \h+and\h+ 匹配 1+ 个水平空格
  • (?=[^{}]*\}) 断言右边是结束语}

Regex demo

与其使用遗漏,不如实施期望特定格式的规则以匹配您提供的示例:

([A-Z]+[A-Za-z]*('[A-Za-z]+)*, [A-Z]? ?[A-Z]+[A-Za-z]*('[A-Za-z]+)*( [A-Z])?)

https://regex101.com/r/9LGqn3/3