在两个问题之间获取答案的正则表达式模式



如何获取问题末尾(从后面开始)和下一个以" question "开头的问题之前的文本?

答案用新行分隔

import re
text = "Which feature is not part of the linux system?
pipe
2) dirx
ls
ps
Question 2 ("
output= re.findall(r'?s*(.*?)s*Question)', splitext).split('n')
print(output)

您可以使用这个正则表达式来匹配?Question之间所需的文本:

(?s)(?<=?).+?(?=nQuestion )

RegEx演示

解释:

  • (?s):启用DOTALL模式以确保.匹配换行符也
  • (?<=?):向后看,断言?就在当前位置
  • 之前
  • .+?:匹配1+任意字符,包括换行符
  • (?=nQuestion ):向前看,断言我们在当前位置前面有一个换行符,后面跟着Question

您可以使用捕获组,匹配中间不以问号结束且不以Question

开头的行
^.*?((?:n(?!.*?$|Questionb).*)+)
  • ^起始字符串
  • .*?匹配以?结尾的行
  • (capturegroup 1(将由re.findall返回)
    • (?:非捕获组作为一个整体重复
      • n(?!.*?$|Questionb)匹配换行符,并断言该行不以?结束或以Question
      • 开头
      • .*如果断言为真,匹配整行
    • )*关闭非捕获组并可选地重复
  • )关闭组1

Regex演示例如

import re
text = ("Which feature is not part of the linux system?n"
"pipen"
"2) dirxn"
"lsn"
"psnn"
"Question 2 (")
output = re.findall(r'^.*?((?:n(?!.*?$|Questionb).*)*)', text)
print(output)

输出
['npipen2) dirxnlsnpsn']