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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Web Development
  3. ASP.NET
  4. RegEx.IsMatch not working!!

RegEx.IsMatch not working!!

Scheduled Pinned Locked Moved ASP.NET
regexsysadmintutorialquestion
5 Posts 2 Posters 2 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
    Sachin Gedam
    wrote on last edited by
    #1

    hello friends, my task is to validate data on server side..by any means... I feel regular expression "RegEx" is a simplest way to do so...so guide me for the same.. now see my regular expression for checking numeric value is "[0-9]*" and syntax for checking so is RegEx.IsMatch(targeted string, reg expression) and will return true and false. RegEx.IsMatch("0","[0-9]) will return true. RegEx.IsMatch("a","[0-9]") will return false. now RegEx.IsMatch("12fa","[0-9]*"); gives me true value, why? I used RegulaIOptins also as a third parameter...can u please correct me if I m doing wrong or give me simplest solution to check whether data is numeric, alphanumeric or alpha only...(priority is RegEx). Eagerly waiting for your reply. Sachin Gedam (Software Engg.) Pune India

    C 2 Replies Last reply
    0
    • S Sachin Gedam

      hello friends, my task is to validate data on server side..by any means... I feel regular expression "RegEx" is a simplest way to do so...so guide me for the same.. now see my regular expression for checking numeric value is "[0-9]*" and syntax for checking so is RegEx.IsMatch(targeted string, reg expression) and will return true and false. RegEx.IsMatch("0","[0-9]) will return true. RegEx.IsMatch("a","[0-9]") will return false. now RegEx.IsMatch("12fa","[0-9]*"); gives me true value, why? I used RegulaIOptins also as a third parameter...can u please correct me if I m doing wrong or give me simplest solution to check whether data is numeric, alphanumeric or alpha only...(priority is RegEx). Eagerly waiting for your reply. Sachin Gedam (Software Engg.) Pune India

      C Offline
      C Offline
      Colin Angus Mackay
      wrote on last edited by
      #2

      Sachin Gedam wrote: RegEx.IsMatch("12fa","[0-9]*"); gives me true value, why? Because you have 3 possible matches: 1 2 12


      Cada uno es artifice de su ventura WDevs.com - Open Source Code Hosting, Blogs, FTP, Mail and Forums

      1 Reply Last reply
      0
      • S Sachin Gedam

        hello friends, my task is to validate data on server side..by any means... I feel regular expression "RegEx" is a simplest way to do so...so guide me for the same.. now see my regular expression for checking numeric value is "[0-9]*" and syntax for checking so is RegEx.IsMatch(targeted string, reg expression) and will return true and false. RegEx.IsMatch("0","[0-9]) will return true. RegEx.IsMatch("a","[0-9]") will return false. now RegEx.IsMatch("12fa","[0-9]*"); gives me true value, why? I used RegulaIOptins also as a third parameter...can u please correct me if I m doing wrong or give me simplest solution to check whether data is numeric, alphanumeric or alpha only...(priority is RegEx). Eagerly waiting for your reply. Sachin Gedam (Software Engg.) Pune India

        C Offline
        C Offline
        Colin Angus Mackay
        wrote on last edited by
        #3

        I'm assuming that what you are trying to get are strings that contain only numbers. Try this:

        Regex re = new Regex(@"\D");
        Match m = re.Match(someText);
        if (m.Success)
        {
        // This is NOT a number.
        }

        This turns the search on its head and looks for things that are not numbers. So a successful match means that the expression contains non-numeric characters. Does this help?


        Cada uno es artifice de su ventura WDevs.com - Open Source Code Hosting, Blogs, FTP, Mail and Forums

        S 1 Reply Last reply
        0
        • C Colin Angus Mackay

          I'm assuming that what you are trying to get are strings that contain only numbers. Try this:

          Regex re = new Regex(@"\D");
          Match m = re.Match(someText);
          if (m.Success)
          {
          // This is NOT a number.
          }

          This turns the search on its head and looks for things that are not numbers. So a successful match means that the expression contains non-numeric characters. Does this help?


          Cada uno es artifice de su ventura WDevs.com - Open Source Code Hosting, Blogs, FTP, Mail and Forums

          S Offline
          S Offline
          Sachin Gedam
          wrote on last edited by
          #4

          yes its working colin, thanks for the solution...but i want to work on that regular expression only...i want to use [0-9]* as a regular expression...i want same code but with this regular expression...to check numbers only... Once again thanks for your kind cooperation. Sachin Gedam (Software Engg.) Pune India

          C 1 Reply Last reply
          0
          • S Sachin Gedam

            yes its working colin, thanks for the solution...but i want to work on that regular expression only...i want to use [0-9]* as a regular expression...i want same code but with this regular expression...to check numbers only... Once again thanks for your kind cooperation. Sachin Gedam (Software Engg.) Pune India

            C Offline
            C Offline
            Colin Angus Mackay
            wrote on last edited by
            #5

            What exactly is it that you want? Because your request does not make sense. From what I can see: You want to test that a string contains numbers and only numbers. Yes or no? If it is "yes", then the code I gave will work by coming at the problem from a different angle. In other words it will match things that are not numbers. Then all you need to do is logically NOT the result of the match operation (i.e. !matchResult). So you need to explain to me what the problem is with the solution I gave. If it is "no", then you need to explain further because I cannot tell from your post what it is that you want.


            Cada uno es artifice de su ventura WDevs.com - Open Source Code Hosting, Blogs, FTP, Mail and Forums

            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