Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Regex.Replace hangs !!!

Regex.Replace hangs !!!

Scheduled Pinned Locked Moved C#
regexhelpquestion
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    sgatto159
    wrote on last edited by
    #1

    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!

    S 1 Reply Last reply
    0
    • S sgatto159

      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!

      S Offline
      S Offline
      S Senthil Kumar
      wrote on last edited by
      #2

      Can you give an example of a string that causes the regex to hang? Regards Senthil _____________________________ My Blog | My Articles | WinMacro

      W 1 Reply Last reply
      0
      • S S Senthil Kumar

        Can you give an example of a string that causes the regex to hang? Regards Senthil _____________________________ My Blog | My Articles | WinMacro

        W Offline
        W Offline
        whizzs
        wrote on last edited by
        #3

        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

        W 1 Reply Last reply
        0
        • W whizzs

          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

          W Offline
          W Offline
          whizzs
          wrote on last edited by
          #4

          my reg ex string did not post correctly it should be @"(?:\[quote\])(?.*)(?:\[/quote\])"

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups