我想将此字符串中的每个 if 结构作为一个单独的结构捕获
if a > b
do this
do that
if a == c
do this
do that
我想匹配
if a > b
do this
do that
和
if a == c
do this
do that
到目前为止,我没有在新的 if 结构之间进行分隔
if(\W+\w+)+\n\t
最佳答案
您可以使用这个基于前瞻的正则表达式:
^(\s*if[\s\S]+?)(?=^\s*if|\z)
在 MULTILINE
模式下。
[\s\S]+
将匹配另外 1 个字符包括换行符 和 (?=^\s*if|\z)
是前瞻性的,它将确保断言当前匹配项旁边是另一个 if
block 或输入结束。
关于c# - 如何捕获带缩进的字符串组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32592995/