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 "Unrecognized Escape Seq?"

Regex "Unrecognized Escape Seq?"

Scheduled Pinned Locked Moved C#
regexhelpquestion
4 Posts 4 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.
  • . Offline
    . Offline
    ...---...
    wrote on last edited by
    #1

    I'm trying to validate a TextBox for a floating point or int value ONLY. Keep getting a compiler error of "Unrecognized Escape Seq". Regex theRegex = new Regex("[-+]?([0-9]*\.)?[0-9]+ "); A) A quicker better way to validate? B) Where is the code incorrect? thanks

    S G H 3 Replies Last reply
    0
    • . ...---...

      I'm trying to validate a TextBox for a floating point or int value ONLY. Keep getting a compiler error of "Unrecognized Escape Seq". Regex theRegex = new Regex("[-+]?([0-9]*\.)?[0-9]+ "); A) A quicker better way to validate? B) Where is the code incorrect? thanks

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

      The '\' right after the * character is what is causing the compiler to baulk. Either escape it ('\\') or make the string verbatim by prefixing the @ symbol (Regex theRegex = new Regex(**@**"[-+]?([0-9]*\.)?[0-9]+ ");) Regards Senthil _____________________________ My Blog | My Articles | WinMacro

      1 Reply Last reply
      0
      • . ...---...

        I'm trying to validate a TextBox for a floating point or int value ONLY. Keep getting a compiler error of "Unrecognized Escape Seq". Regex theRegex = new Regex("[-+]?([0-9]*\.)?[0-9]+ "); A) A quicker better way to validate? B) Where is the code incorrect? thanks

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

        A. Specify the RegexOptions.Compiled flag when you create the object if you plan to use it more than once. It takes longer to create the object, but it runs faster. B. As regular expressions and C# strings use the same escape character, you have to escape the escape character. Use \\ to put \ in the string. Also, you are missing the ^ and @ to specify the beginning and end of the string. Without them, any string containing a number will be valid, for an example the string "We have 200 horses.". You have an extra space at the end of the pattern. Remove that. [0-9] can also be written as \d. Regex theRegex = new Regex("^[-+]?(\\d*\\.)?\\d+@"); Or you can use an @-quoted string: Regex theRegex = new Regex(@"^[-+]?(\d*\.)?\d+@"); --- b { font-weight: normal; }

        1 Reply Last reply
        0
        • . ...---...

          I'm trying to validate a TextBox for a floating point or int value ONLY. Keep getting a compiler error of "Unrecognized Escape Seq". Regex theRegex = new Regex("[-+]?([0-9]*\.)?[0-9]+ "); A) A quicker better way to validate? B) Where is the code incorrect? thanks

          H Offline
          H Offline
          hamster1
          wrote on last edited by
          #4

          The problem is the "\." - characters after the backslash are interpreted Escape Sequence, which in your case makes (luckily) no sense. Two ways to fix it: Regex theRegex = new Regex("[-+]?([0-9]*\\.)?[0-9]+ "); Regex theRegex = new Regex(@"[-+]?([0-9]*\.)?[0-9]+ "); ---------------------- ~hamster1

          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