javascript - 需要正则表达式帮助

标签 javascript regex text

我正在使用正则表达式来查找答案选项及其描述。 到目前为止,它工作得很好,但问题是我无法弄清楚如何在不包含下一个选择的情况下包含多行描述。

我当前的正则表达式:

^(Choice [A-I] \(.*\) +(?:is incorrect. )?($|\r?.*))

测试用例:

Choice B (Predominance of eosinophils) zfdfdfbhdfdfdf
fgdfgdfgdfdfdfhd fgdfgdfgdsgsf
sgsgdfgdf gdfgdfgdfgdfgd gdfg
Choice C (Monosodium urate crystals) fghfdghfghfghfh
Choice D (Spirochetes) is incorrect fghfghfghfghfghf
Choice E (Predominance of polymorphonuclear cells) 

Demo on regex101

我需要选项 B 后面的两个随机文本句子也包含在选项 B 匹配中,但不包含选项 C。

最佳答案

你可以尝试:

(Choice [A-I] \(.*\) )(((?!Choice)[^\n]+[\n]?)+)?

Demo on regex101

说明

  • (?!Choice):字符串不以“Choice”开头

  • [^\n]:除“\n”之外的任何字母

关于javascript - 需要正则表达式帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32980947/

相关文章:

python - 当未找到子字符串 B 时,正则表达式仅匹配子字符串 A

java - 如何获取节点周围的文本?

c++ - 写很多txt文件(90),每个文件5MB,耗时1400s

javascript - jQuery .blur 在 Firefox 中不起作用

JavaScript 添加天数不增加月份

javascript - 如何从 JSON 对象的值数组中检索键?

javascript - Google Apps 脚本 - TypeError : Cannot read property "1" from null

regex - 使用负先行正则表达式的 Sed 替换

javascript - 按键时水平滚动整页

python - wxPython 将文本绘制到现有位图或图像上