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

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
 
M

Magne Ryholt

Have you tried "^<%!--(?<val>.*)--%>$" ?
^ means beginning of line and $ means end of line
 
M

Michael Lang

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
 
K

Ken Arway

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);
 
M

Michael Lang

Thanks!!! That is exactly what I was looking for. I've tested it
with various input files and it works as expected.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top