Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 条件正则表达式模板_Regex_Regex Group - Fatal编程技术网

Regex 条件正则表达式模板

Regex 条件正则表达式模板,regex,regex-group,Regex,Regex Group,我想使用正则表达式在模板上包含或排除条件文本 一个简单的模板: #IF {{Placeholder}} If true the text should remain, else it should be deleted #ENDIF 在第一次运行时,我将占位符替换为简单的true或false字符串: #IF false If true the text should remain, else it should be deleted #ENDIF 在第二次运行时,我使用以下正则表达式应用条件

我想使用正则表达式在模板上包含或排除条件文本

一个简单的模板:

#IF {{Placeholder}}
If true the text should remain, else it should be deleted
#ENDIF
在第一次运行时,我将占位符替换为简单的true或false字符串:

#IF false
If true the text should remain, else it should be deleted
#ENDIF
在第二次运行时,我使用以下正则表达式应用条件:

Regex.Replace(input, "(?s)#IF false.*?#ENDIF\r\n", "", RegexOptions.Compiled);
如果占位符为“false”,则将按预期删除全文块

现在缺少的是“true”条件,这是一个保留内部文本的正则表达式,只删除该组的“#IF true”和“#ENDIF”行

If true the text should remain, else it should be deleted

有什么想法吗?

给你,假设我在这里所做的一切都将被删除。如果为真,则删除注释,如果为假,则删除整个块。所以考虑到这一点,我们做了一个正则表达式。它满足您的两个要求。如果为true,则只删除注释,如果为false,则删除整个块


正则表达式:
”(?:(?:#IF\s(?:false)\n.*))|(?:#IF true\n)(?只要捕获IF和ENDIF之间的行,并将其值用作替换:

(?s)#IF\s*false.*?#ENDIF|#IF\s*true\R(.+?)\R#ENDIF

你真的必须发明自己的模板框架吗?这是一个可怕的错误想法,但是你的
if-true
部分只想捕获
#ENDIF
之前的所有内容,并用捕获的部分替换字符串:
r=Regex.Compile(?s)#if-true(.*)ENDIF\r\n“;r.replace(输入,m=>m.Groups[1].Value)
。但是,现在已经有了很好的模板系统。请改用其中的一个。谢谢您的帮助:)我知道有很多模板框架,但我只需要一个简单的解决方案来解决一个简单的问题,而不需要任何开销和性能问题。