Regex.Replace hangs !!!
-
Hi all, I'm trying to write some sort of forum with "forumcode" like snitz. To quote some text, user can surround it with [quote]...[/quote]. To do this I have written this regex:
@"(?<quo_iniTag>\[quote\])(?<quo_text>.*)(?<quo_endTag>\[/quote\])"
and the handler is something like this:
if(m.Groups["quo_iniTag"].Value.Equals("") == false && m.Groups["quo_endTag"].Value.Equals("") == false)
return "####"+m.Groups["quo_text"].Value+"###";But, sometimes (not always), it hangs forever on the method Replace. Can you help me please ? Thanks in advance ! ---------------------- !happy coding!
-
Hi all, I'm trying to write some sort of forum with "forumcode" like snitz. To quote some text, user can surround it with [quote]...[/quote]. To do this I have written this regex:
@"(?<quo_iniTag>\[quote\])(?<quo_text>.*)(?<quo_endTag>\[/quote\])"
and the handler is something like this:
if(m.Groups["quo_iniTag"].Value.Equals("") == false && m.Groups["quo_endTag"].Value.Equals("") == false)
return "####"+m.Groups["quo_text"].Value+"###";But, sometimes (not always), it hangs forever on the method Replace. Can you help me please ? Thanks in advance ! ---------------------- !happy coding!
Can you give an example of a string that causes the regex to hang? Regards Senthil _____________________________ My Blog | My Articles | WinMacro
-
Can you give an example of a string that causes the regex to hang? Regards Senthil _____________________________ My Blog | My Articles | WinMacro
Not it is a big deal, but I would clean the code up a bit by using non-capturing groups for the [quote] and [/quote] because you just want to see if they are there, but don't need them. So the regex string would be: @"(?:\[quote\])(?.*)(?:\[/quote\])" if ( m.success ) { return "####"+m.Groups["quo_text"].Value+"###"; } then I would change the if statement to a while loop in case the user has more than 1 quote block in a line string [] s = new string[]; int i = 0; while ( m.success ) { s [i] = "####"+m.Groups["quo_text"].Value+"###"; m.NextMatch(); } return s; But please send an example of the string that causes it to hang
-
Not it is a big deal, but I would clean the code up a bit by using non-capturing groups for the [quote] and [/quote] because you just want to see if they are there, but don't need them. So the regex string would be: @"(?:\[quote\])(?.*)(?:\[/quote\])" if ( m.success ) { return "####"+m.Groups["quo_text"].Value+"###"; } then I would change the if statement to a while loop in case the user has more than 1 quote block in a line string [] s = new string[]; int i = 0; while ( m.success ) { s [i] = "####"+m.Groups["quo_text"].Value+"###"; m.NextMatch(); } return s; But please send an example of the string that causes it to hang