Regex 使用正则表达式匹配多项选择?

Regex 使用正则表达式匹配多项选择?,regex,dreamweaver,Regex,Dreamweaver,如何从文本文档中提取多项选择题。每个问题都以数字和点开头 1. Any Text _____ Goes here, And end with ? Or . And also can contain another paragraph. a) possible b) use regex c) not possible d) I dont know Ans: b 上面是一个问题的例子。文本文件包括一些填空和一些论文写作材料,但我只想选择题部分,直到Ans:…。所有问题都有答案a、b、c和d 我在

如何从文本文档中提取多项选择题。每个问题都以数字和点开头

1. Any Text _____ Goes here, And end with ? Or . And also can contain another paragraph.
a) possible
b) use regex
c) not possible
d) I dont know

Ans: b
上面是一个问题的例子。文本文件包括一些填空和一些论文写作材料,但我只想选择题部分,直到
Ans:…
。所有问题都有答案a、b、c和d

我在Dreamweaver中复制了我的文本,以便可以使用正则表达式

“1.任何文本都在此处,并以?或结尾。”

可以在正则表达式中转换为:

    \d+\.[^\?\.]*[\?\.]

这对你有用吗?这假设在问题结束之前,你从来没有任何问号或句号。。。但这也是你暗示的

编辑:由于您想要的是答案,而不仅仅是问题本身,并且您希望区分其他类型的问题,请尝试以下方法:

([ \t]*\d+\.[^\n]+\n(?:[ \t]*[a-zA-Z]\)[^\n]+\n)+[\s]*Ans:[^\n]*)

说明 此表达式将:

  • 将整个问题捕获到组0中的答案
  • 将问题编号记录到组1中
  • 将问题的文本捕获到第2组中
  • 捕获第3组的可能答案
  • 捕获组4的答案值
  • 允许所有标点符号,包括问号
^(\d+)。\s*(.*)[\r\n\s]+(^a\).*?[\r\n\s]+Ans:\s+([a-z]+\b)

例子 有关工作示例,请参见此处:

示例文本

12. Any Text _ Goes here, And end with ? Or . And also can contain another paragraph.
a) Q1 possible
b) Q1 use regex
c) Q1 not possible
d) Q1 i dont know

Ans: a

Do you like kittens or other random text?

24. Second question is here
a) Q2 possible
b) Q2 use regex
c) Q2 not possible
d) Q2 i dont know

Ans: b
匹配

Match 1
1.  12
2.  Any Text _ Goes here, And end with ? Or . And also can contain another paragraph.
3.  a) Q1 possible
    b) Q1 use regex
    c) Q1 not possible
    d) Q1 i dont know
4.  a

Match 2
1.  24
2.  Second question is here
3.  a) Q2 possible
    b) Q2 use regex
    c) Q2 not possible
    d) Q2 i dont know
4.  b
笔记
这个正则表达式假设每个问题的末尾都有一个
Ans:x

非常感谢您的回复,那么中间部分呢。。。回答:…你怎么能画这样的统计图?问题是如果你在两个问题之间添加一个随机文本,它也会选择随机文本。这一个也会选择一个问题,比如说填空。是否有可能限制该问题必须有类似的选项。。。b) 。。。c) 。。。d) …啊,我不知道你想要它有多具体,还有其他的可能性。。我会更新的