开发者

Regular Expression - How to find the <%@ %> line in the file?

开发者 https://www.devze.com 2023-01-14 06:46 出处:网络
I found there is a bug in this highlight editor: http://cshe.ds4a.com/ The following ASP.Net code can\'t be highlighted correctly

I found there is a bug in this highlight editor: http://cshe.ds4a.com/

The following ASP.Net code can't be highlighted correctly

<%@ Page Title="<%$ Resources: XXX %>" Language="C#" ContentType="text/html" ResponseEncoding="utf-8" %>

The problem is about the regular expression, how can I find this whole line by regular expression?

I am using the RegExp from ActionScript3

The main challenges are:

  1. The <%@ %> instruction may contains another <%$ %> instruction in its attribute, just like the one above

  2. The <%@ %> instruction may have a line break in it, just like the following.

<%@ Page Title="<%$ Resources: XXX %>"
Language="C#" ContentType="text/html" ResponseEncoding="utf-8"
 %>

3 . The <%@ %> instruction may followed by another <%@ %> without any space / line-break

<%@ Page Title="<%$ Resources: XXX %>"
Language="C#" ContentType开发者_运维百科="text/html" ResponseEncoding="utf-8"
 %><%@ Import Namespace="System" %>

Thank you


I'm not sure all these escapes are necessary, but I kept them for good meassure. This found your line in notepad++ find

^<\%\@.*\%>$

EDIT

For multiple lines, set the multiline and dotall flags. Those inform that the expression should span over several lines, and that the . wildcard should match newline (\n).

/<\%\@.*\%>/sm

or

<\%\@.*\%>

With s and m flags.


Try this:

/<%@[^%"']++(?:(?:%(?!>)|"[^"]*+"|'[^']*+')[^%"']++)*+%>/

Anything that's enclosed in double-quotes or single-quotes is treated as generic string content, so a %> in an attribute value won't prematurely close the tag for matching purposes.


Based on the headline I created a little RegEx which also takes care of whitepasce at the start or end of the file. However I can not assure if this fits into your project.

^\s*<%@.*>\s*$

I tested this with the PHP function preg_match_all()

Update:

Use this pattern for a across multiple lines. Your RegExLibrary has to support the parameter "s" (which accepts newlines as character) though

/\s*<%@.*>\s*/s
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号