Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么这个正则表达式在第一个捕获组再次出现之前不匹配所有内容?_Python_Regex_Multiline_Citations_Multilinestring - Fatal编程技术网

Python 为什么这个正则表达式在第一个捕获组再次出现之前不匹配所有内容?

Python 为什么这个正则表达式在第一个捕获组再次出现之前不匹配所有内容?,python,regex,multiline,citations,multilinestring,Python,Regex,Multiline,Citations,Multilinestring,我该怎么做呢 现在它在换行时停止(就像在“芝加哥”之后)。 或者,如果我使用DOTALL,它只匹配“Abbott A(1988)”,然后匹配字符串的其余部分,直到最后。 我希望它在下一次出现([\w\s]+)([1 | 2]\d{3})时停止,即。。。“Albu OB和Flyverbom M(2016年)”。等等等等 欢迎指点 pattern = r"(([\w\s]+)\(([1|2]\d{3})\))(.*)" 样本串 "Abbott A (1988) The System of Prof

我该怎么做呢

现在它在换行时停止(就像在“芝加哥”之后)。 或者,如果我使用DOTALL,它只匹配“Abbott A(1988)”,然后匹配字符串的其余部分,直到最后。 我希望它在下一次出现([\w\s]+)([1 | 2]\d{3})时停止,即。。。“Albu OB和Flyverbom M(2016年)”。等等等等

欢迎指点

pattern = r"(([\w\s]+)\(([1|2]\d{3})\))(.*)"
样本串

"Abbott A (1988) The System of Professions: An Essay on the Division of Expert Labor. Chicago,
IL: University of Chicago Press.
Albu OB and Flyverbom M (2016) Organizational transparency: conceptualizations, con-
ditions, and consequences. Business & Society. Epub ahead of print 13 July. DOI:
10.1177/0007650316659851.
Ananny M (2016) Toward an ethics of algorithms: convening, observation, probability, and timeli-
ness. Science, Technology & Human Values 41(1): 93–117. DOI: 10.1177/0162243915606523."
沙箱

您可以使用

(?sm)^([^()\n\r]+)\(([12]\d{3})\)(.*?)(?=^[^()\n\r]+\([12]\d{3}\)|\Z)

详细信息

  • (?sm)
    -
    re.DOTALL
    re.MULTILINE
    已启用
  • ^
    -行的开头
  • ([^()\n\r]+)
    -第1组:除
    、CR和LF之外的一个或多个字符
  • \(
    -a
  • ([12]\d{3})
    -第2组:
    1
    2
    ,然后是任意3位数字
  • \)
    -a
    字符
  • (.*)
    -第3组:任何0+字符,包括换行符,尽可能少,最多(但不包括在匹配项中)第一个字符
  • (?=^[^()\r\n]+\([12]\d{3}\)\124;\ Z)
    -(一种积极的前瞻性,需要在当前位置的右侧立即显示其模式):
    • ^[^()\r\n]+\([12]\d{3}\)
      -与模式的开头相同,但没有组
    • |
      -或
    • \Z
      -全文结束

因为您不使用多行标志?我使用了。它在沙盒中打开,很抱歉没有写下来。我添加了一个逗号,像这样(?sm)^([\w\s,]+)(([12]\d{3}))(.*)(?(=^[\w\s,]+([12]\d{3})|\Z)来包含那些具有多个属性的引用authors@treakec我建议用
[^()\n\r]+
来解决这个问题(逗号不止,点也有)。它匹配任何字符,但不匹配
或公共换行字符。是的,好主意。我想以后它也会派上用场的。谢谢