vs.net 2005 regular expression infinite loop...

G

Guest

I have a aspx file (snippet shown below):

=======
<td class="light-m1" id="rwCompleteButton" runat="server"><br/>
<asp:ImageButton CssClass="clear-m1" runat="server"
CommandName="Complete" ID="btnComplete" />

<asp:ImageButton CssClass="clear-m1" runat="server"
CommandName="Delete" ID="btnDelete" />
</td>
=======

and the following .cs file to execute (snippet below)

========
strReg = @"(<\w+:(.|[\r\n])*?id=""btnComplete"")([^>]+>)";
reg = new Regex(strReg);
if (!reg.IsMatch(uiStr))
{
blah...blah...
}
========

The if statement above is going to infinite loop. However, when I try the
expression on Expresso (a third party tool written in vs.net 2003) it matches
just fine.

Any ideas?
 
G

Guest

sorry forgot to mention that I am first reading the aspx file in the uiStr
before executing this code.

Thanks
 
S

Steven Cheng[MSFT]

Hi Jojobar,

Welcome to ASP.NET newsgroup.

As for the regex problem you mentioned, I think you can first test the
regex through the .net regulator tool which is a pure .net developed regex
testing tool:

http://sourceforge.net/project/showfiles.php?group_id=105210

also, from the code you provided, you used the following regex expression:

strReg = @"(<\w+:(.|[\r\n])*?id=""btnComplete"")([^>]+>)";

based on my test, the original regex should be
(<\w+:(.|[\r\n])*?id="btnComplete")([^>]+>) and you use "" to escape " ,
yes?
If so, this will not work in .net because "" is not the correct escape for
" in C#, you may consider use

strReg = "(<\\w+:(.|[\\r\\n])*?id=\"btnComplete\")([^>]+>)" , anyway
manually replace each particular char with the escaped one in C# format.

Hope this helps.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
G

Guest

Hello Steven,

Thank you for your time. I tested it with a tool called expresso which is
(in my opinion) a better regex evaluator tool.

Also the two "" you mentioned is there because I used a string literal to
escape the quotes. For exanmple

str = @"This is ""a''" test"
and
str = "This is \"a\" test"

are equivalent.

BTW I changed my expression to the second form and it still goes inside
infinite loop!


Thanks
 
S

Steven Cheng[MSFT]

Thanks for your response Jojobar,

I think the problem could be related to the RegexOptions we set to the
Match function or regex's construtor. I've just performed the test in a
..net 2.0 winform application and it can work. The test code is like below:

============================

string doc = .................;

string regex = "(<\\w+:(.|[\\r\\n])*?id=\"btnComplete\")([^>]+>)";

MatchCollection matchs = Regex.Matches(doc, regex, RegexOptions.Compiled |
RegexOptions.IgnoreCase);

MessageBox.Show(matchs.Count.ToString());
======================

You can also have a test on your side to see whether this is the case.

Hope this helps.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
G

Guest

Yes it works now. Thanks for your help. I think the options
made it to work. Maybe the infinite loop thing need to be reported to the ms
development anyways.

Thanks
 
G

Guest

Hello,

I just tested it again, I find that with the example you have given, it is
also going into infinite loop.

Here is the code:

string uiStr = Utils.OCFile.ReadTextFile("taskcontrol.ascx");

string regex = "(<\\w+:(.|[\\r\\n])*?id=\"btnRecurrenceAdd\")([^>]+>)";

MatchCollection matchs = Regex.Matches(uiStr, regex, RegexOptions.Compiled
| RegexOptions.IgnoreCase);

if (matchs.Count == 0)
{
MessageBox.Show("Failed.");
}
else
{
MessageBox.Show("Success");
}

The statement matchs.Count is going into infinite loop

Note that the file taskcontrols.ascx is little big to put here, so it is in
http://www.my-email-signature.com/Test/test.zip and you can download it from
there.


--
-jojobar


jojobar said:
Hello Steven,

Thank you for your time. I tested it with a tool called expresso which is
(in my opinion) a better regex evaluator tool.

Also the two "" you mentioned is there because I used a string literal to
escape the quotes. For exanmple

str = @"This is ""a''" test"
and
str = "This is \"a\" test"

are equivalent.

BTW I changed my expression to the second form and it still goes inside
infinite loop!


Thanks
--
-jojobar


Steven Cheng said:
Hi Jojobar,

Welcome to ASP.NET newsgroup.

As for the regex problem you mentioned, I think you can first test the
regex through the .net regulator tool which is a pure .net developed regex
testing tool:

http://sourceforge.net/project/showfiles.php?group_id=105210

also, from the code you provided, you used the following regex expression:

strReg = @"(<\w+:(.|[\r\n])*?id=""btnComplete"")([^>]+>)";

based on my test, the original regex should be
(<\w+:(.|[\r\n])*?id="btnComplete")([^>]+>) and you use "" to escape " ,
yes?
If so, this will not work in .net because "" is not the correct escape for
" in C#, you may consider use

strReg = "(<\\w+:(.|[\\r\\n])*?id=\"btnComplete\")([^>]+>)" , anyway
manually replace each particular char with the escaped one in C# format.

Hope this helps.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
S

Steven Cheng[MSFT]

Thank for your followup jojobar,

Really feel a bit suprised. Is this hang behavior occuring both in a
winform application and ASP.NET application? If it's a common issue, I'll
try reproducing and try contacting some other framework guys to see whether
this is an known issue. At least this should be a big problem.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
G

Guest

It is happening in winforms program. I suspect this may happen in a console
program too.
 
S

Steven Cheng[MSFT]

Thanks for your response.

Well, I've perfomed the test through the file you attached on the site and
I managed to reproduce the problem. Actually the "regulator" tool also
somewhat suffers this issue. I agree that there should be something
internally cause this behavior(and the file you provided just fire this
issue). IMO, due to the limitation of our newsgroup support resource, such
kind of issue should be better to be handled by CSS which can leverage
further resource and provide other thorough troubleshooting on this (like
dump analysis). therefore, if you feel this an urgent issue to resolve or
get a workaround, I suggest contact CSS for further assistance.

Thanks for your understanding.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
G

Guest

I am not sure how and where to contact css. So I will try to find an
workaround. Thanks for the help. Maybe you can pass this email to the
developer community in MS and this bug can be fixed.

Thanks
 
S

Steven Cheng[MSFT]

Thanks for your followup.

If necessary, you can contact the CSS through the following link:

http://support.microsoft.com/

Of course, I can help forward this to our internal dicussion group so that
some dev engineer can see this issue. In addition, I also suggest you
posting this to the MSDN product feedback center:

http://lab.msdn.microsoft.com/productfeedback/default.aspx

Thanks for your understanding.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 

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

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top