如何否定特定的正则表达式,如果行开始与它?



我必须在:note:之后得到一个文本:

Command description
:note: Command note
another line of command note
:example: Command example
:example: Another command example
:argname arg: argument
:argdesc arg: argument description
:argreq arg: true
:argnote arg: Argument note

结果应该是"Command note另一行命令注释">

我原来的表达:re.findall(r":note: (.+)", self.help)

我试图使用负向前看(?!),但我不知道如何在这种情况下使用它

您可以像这样使用反向查找,匹配不以:w+:

开头的下一行。组值将由re.findall

返回
:note: (.+(?:r?n(?!:[^:rn]+:).*)*)

模式匹配:

  • :note:逐字匹配
  • (第1组
    • .+匹配除换行符外的1+字符
    • (?:非捕获组
      • r?n(?!:[^:rn]+:).*匹配换行符,并使用负向前看断言该行不以:开头1+倍于除:或换行符以外的任何字符,然后匹配:
    • )*关闭非捕获组并可选地重复以匹配所有行
  • )关闭组1

Regex演示

相关内容

最新更新