如何使用regex lookbehind只匹配一次?

qnzebej0  于 2023-05-30  发布在  其他
关注(0)|答案(1)|浏览(103)

我想构建一个正则表达式,它只直接匹配条件之前的内容,像这样:

Question: What is the capital city of France?
A. Berlin
B. Paris
C. Rome
D. Madrid

Key: B

Question: Who is credited with inventing the World Wide Web?
A. Steve Jobs
B. Bill Gates
C. Tim Berners-Lee
D. Mark Zuckerberg

Key: C

我想匹配:

A. Berlin
C. Rome
D. Madrid

A. Steve Jobs
B. Bill Gates
D. Mark Zuckerberg

关键字是A -> match:B C D。
关键是B -> match:A,C,D
关键是C -> match:A B D
关键是D -> match:A,B,C
这是当键是C时的正则表达式:

(?<!Key: C)^[ABD].*

但它会匹配:

A. Berlin
B. Paris
D. Madrid

A. Steve Jobs
B. Bill Gates
D. Mark Zuckerberg

有谁能就如何解决此问题提出解决方案或提供指导吗?

bwntbbo3

bwntbbo31#

你可以使用lookahead来Assert一个答案后面没有一个具有相同键的Key:行:

^                           # Match an answer that starts at the start of line,
(?<key>[ABCD])              # then a key, which we capture,
\.\s+.+                     # a dot, some spaces and everything else to the end of line,
(?=                         # followed by
  (?:\n[ABCD]\.\s+.+){0,3}  # 0 to 3 more answers, then
  \n                        # a blank line,
  \nKey:\s+                 # then 'Key:' succeeded by some spaces and
  (?!\k<key>)               # something that is not the same as the key we captured.
)

同样的正则表达式也可以在Python中使用,只是在语法上有一些细微的差异:

^(?P<key>[ABCD])\.\s+.+
(?=
  (?:\n[ABCD]\.\s+.+){0,3}
  \n
  \nKey:\s+(?!(?P=key))
)

在www.example.com上试试regex101.com:PCRE/PCRE2/Java 8/.NETECMAScriptPython

相关问题