简体   繁体   English

正则表达式多行

[英]Regex multiline

I'm trying to highlight everything between '<%' and '%>' with a richtextbox. 我正在尝试通过富文本框突出显示“ <%”和“%>”之间的所有内容。 (for example, putting every javascript code in blue) (例如,将每个javascript代码都显示为蓝色)

Every other highlighting function works so far, but these are singleline highlights. 到目前为止,所有其他突出显示功能都可以使用,但是这些都是单行突出显示。 I've found the right code to do multilines, but I think my regex is wrong at this point. 我已经找到了执行多行代码的正确代码,但是我认为我的正则表达式在这一点上是错误的。

Any suggestions? 有什么建议么?

Used regex: 使用的正则表达式:

@"\\<\\%(.*?)\\%\\>"

example code: 示例代码:

textextextext
<%
this is javascript code
%>
textextextextextextextext

desired result: 预期结果:

<% 
this is javascript code 
%>

You need to add DOTALL( s ) modifier and also you don't need to escape < , % symbols in the regex. 您需要添加DOTALL( s )修饰符,并且也不需要在正则表达式中转义<%符号。 (?s) modifier makes dot in the regex to match even newline character also. (?s)修饰符可使正则表达式中的点也匹配换行符。

(?s)<%.*?%>

DEMO DEMO

C# code would be, C#代码应该是

String input = @"textextextext
<%
this is javascript code
%>
textextextextextextextext";
Regex rgx = new Regex(@"(?s)<%.*?%>");
foreach (Match m in rgx.Matches(input))
{
Console.WriteLine(m.Groups[0].Value);
}

IDEONE IDEONE

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM