简体   繁体   中英

Regex multiline

I'm trying to highlight everything between '<%' and '%>' with a richtextbox. (for example, putting every javascript code in blue)

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. (?s) modifier makes dot in the regex to match even newline character also.

(?s)<%.*?%>

DEMO

C# code would be,

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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