Using a regular expression to parse comments from aspx (ASP.NET) files

  • Thread starter Thread starter Michael Lang
  • Start date Start date
M

Michael Lang

I need a simple way to parse comment lines from aspx (ASP.NET) files;
in which comments are surounded by

<%!-- and --%>

So I created a simple regex:

<%!--(?<val>.*)--%>

With 'singleline' option on, and the following as input...

<%!-- single line comment --%>
<table>table contents here</Table>
<%!-- multiline
wrapped
comment
--%>

.... and the entire input was the match.

I've also tried:
<%!--(?<val>[^(--%>)]*)--%>

But this excludes any comment that has a dash, percent, OR greater
than sign as part of the comment text.

I've also tried lookbehind assertions with no success. But maybe I'm
doing it wrong?

<%!--(?<val>.*(?<!--%>))--%>

If anyone has an idea what I am doing wrong, please let me know.

Thanks,
Michael Lang
 
Have you tried "^<%!--(?<val>.*)--%>$" ?
^ means beginning of line and $ means end of line
 
No. I want comments that also happen to span more than one line. I
also want to FIND the match in a larger string context, not just see
if a single line is a comment.

Magne Ryholt said:
Have you tried "^<%!--(?<val>.*)--%>$" ?
^ means beginning of line and $ means end of line
Michael Lang said:
I need a simple way to parse comment lines from aspx (ASP.NET) files;
in which comments are surounded by

<%!-- and --%>

So I created a simple regex:

<%!--(?<val>.*)--%>

With 'singleline' option on, and the following as input...

<%!-- single line comment --%>
<table>table contents here</Table>
<%!-- multiline
wrapped
comment
--%>

... and the entire input was the match.

I've also tried:
<%!--(?<val>[^(--%>)]*)--%>

But this excludes any comment that has a dash, percent, OR greater
than sign as part of the comment text.

I've also tried lookbehind assertions with no success. But maybe I'm
doing it wrong?

<%!--(?<val>.*(?<!--%>))--%>

If anyone has an idea what I am doing wrong, please let me know.

Thanks,
Michael Lang
 
Michael said:
I need a simple way to parse comment lines from aspx (ASP.NET) files;
in which comments are surounded by

<%!-- and --%>

So I created a simple regex:

<%!--(?<val>.*)--%>

With 'singleline' option on, and the following as input...

<%!-- single line comment --%>
<table>table contents here</Table>
<%!-- multiline
wrapped
comment
--%>

... and the entire input was the match.

Use the non-greedy quantifier and treat the input as one string. The
"Singleline" option allows the "." to match the newline character. (The
following example also does not capture the leading and trailing spaces.)

Regex regex = new Regex(@"
<%!--\s*(?<val>.*?)\s*--%>",
RegexOptions.IgnoreCase |
RegexOptions.Singleline);
 
Thanks!!! That is exactly what I was looking for. I've tested it
with various input files and it works as expected.
 
Back
Top