views:

56

answers:

3

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

+2  A: 

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.

Codemonkey
Thanks, but actually this line can have a line-break in it, just like <%@ Page Title="<%$ Resources: XXX %>" Language="C#" ContentType="text/html" ResponseEncoding="utf-8" %><%@ Import Namespace="System.Data" %>, I don't think your expression will works :)
Jerry
Then you have to set the multi line flag. Depending on your software, usually that's done by adding "n" behind the end delimiter.
Codemonkey
That is not just about multi-line switch, I am using thr RegExp from ActionScript3, The <%@ %> instruction may contain line break; The instruction attribute may contain another <%$ %>, and <%@ %> may followed by another <%@ %> without any space / line-break
Jerry
Then you should consider trying a programmatical solution, using more than one regex if necessary.
Codemonkey
Thank you for your kindly help :)
Jerry
@Codemonkey: The multiline flag is irrelevant; all it does is change the behavior of the `^` and `$` anchors, which aren't being used here.
Alan Moore
A: 

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
Vash
+1  A: 

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.

Alan Moore