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. Error Evaluating Regular Expression

Error Evaluating Regular Expression

Scheduled Pinned Locked Moved C#
regexhelptutorialquestion
6 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.
  • A Offline
    A Offline
    abhinish
    wrote on last edited by
    #1

    hi guys ! need some help.. i want to match whole words in my string using regular expression i m using Regex.IsMatch function for this and using "\b" as start and end tags for matching the word as a whole every thing is working fine but if some special character comes with the words like '***SPAM***' it throws an exception "Argument Exception , nested quantifier?" can any one help me how to deal with this as i also want these special characters to be matched. moreover there could be any special character which could occur n numver of times in the word. how to deal with that.

    abhinav

    G 1 Reply Last reply
    0
    • A abhinish

      hi guys ! need some help.. i want to match whole words in my string using regular expression i m using Regex.IsMatch function for this and using "\b" as start and end tags for matching the word as a whole every thing is working fine but if some special character comes with the words like '***SPAM***' it throws an exception "Argument Exception , nested quantifier?" can any one help me how to deal with this as i also want these special characters to be matched. moreover there could be any special character which could occur n numver of times in the word. how to deal with that.

      abhinav

      G Offline
      G Offline
      Guffa
      wrote on last edited by
      #2

      Use the Regex.Escape method to escape the characters in the string, so that you can use it in the regular expression.

      --- b { font-weight: normal; }

      A 1 Reply Last reply
      0
      • G Guffa

        Use the Regex.Escape method to escape the characters in the string, so that you can use it in the regular expression.

        --- b { font-weight: normal; }

        A Offline
        A Offline
        abhinish
        wrote on last edited by
        #3

        thanx for the reply guffa but can u explain it with a small example if possible. thanx again

        abhinav

        E G 2 Replies Last reply
        0
        • A abhinish

          thanx for the reply guffa but can u explain it with a small example if possible. thanx again

          abhinav

          E Offline
          E Offline
          Ed Poore
          wrote on last edited by
          #4

          A possibly easier solution would be to write the string (if hard-coded in the code) as: string regex = @"regex-goes-here"; The @ symbol tells csc not to escape the C# characters like '\n', '\t' etc. (The Regex class can do this anyway when passed a literal string.)


          Formula 1 - Short for "F1 Racing" - named after the standard "help" key in Windows, it's a sport where participants desperately search through software help files trying to find actual documentation. It's tedious and somewhat cruel, most matches ending in a draw as no participant is able to find anything helpful. - Shog9 Ed

          1 Reply Last reply
          0
          • A abhinish

            thanx for the reply guffa but can u explain it with a small example if possible. thanx again

            abhinav

            G Offline
            G Offline
            Guffa
            wrote on last edited by
            #5

            Example:

            // A text that contains codes that would mess up a regex
            string word = @"c:\dir\body.txt";
            // Create a pattern containing the text
            string pattern = @"\b" + Regex.Escape(word) + @"\b";
            // Create a Regex object using the pattern
            Regex re = new Regex(pattern);

            --- b { font-weight: normal; }

            A 1 Reply Last reply
            0
            • G Guffa

              Example:

              // A text that contains codes that would mess up a regex
              string word = @"c:\dir\body.txt";
              // Create a pattern containing the text
              string pattern = @"\b" + Regex.Escape(word) + @"\b";
              // Create a Regex object using the pattern
              Regex re = new Regex(pattern);

              --- b { font-weight: normal; }

              A Offline
              A Offline
              abhinish
              wrote on last edited by
              #6

              Hi! thanks again for the reply guys. i tried the Regex.Escape method. it works good and don't throw any exception but it do not match the whole word also. m i again missing some thing.. here is my code:

              public bool sSearchWholeWord() { string sSearchWord="***SPAM***"; string content="hi this is ***SPAM*** testing"; string pattern = @"\b" + Regex.Escape(sSearchWord) + @"\b"; Regex re =new Regex(pattern,RegexOptions.IgnorePatternWhitespace); try { if(re.IsMatch(content)) { return true; } else { return false; } } catch(Exception e) { Response.Write(e.ToString()); return false; } }

              this returns false but according to me it should return true i dont know what m i missing

              abhinav

              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