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. General Programming
  3. C#
  4. Regex: Very basic question

Regex: Very basic question

Scheduled Pinned Locked Moved C#
questionregex
8 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.
  • M Offline
    M Offline
    matthias s 0
    wrote on last edited by
    #1

    Why does this match?

    string sSearch = "-- --";
    const string sRegEx = "[a-z]*[A-Z]*[0-9]*";
    if (Regex.IsMatch(sSearch, sRegEx)) {
    // do something funny
    }

    My understanding is, it should allow any number of characters in the range of a-z, A-Z or any number between 0-9. I guess there is something basic I'm missing out here :~ /matthias

    I love deadlines. I like the whooshing sound they make as they fly by.
    [Douglas Adams]

    D L 2 Replies Last reply
    0
    • M matthias s 0

      Why does this match?

      string sSearch = "-- --";
      const string sRegEx = "[a-z]*[A-Z]*[0-9]*";
      if (Regex.IsMatch(sSearch, sRegEx)) {
      // do something funny
      }

      My understanding is, it should allow any number of characters in the range of a-z, A-Z or any number between 0-9. I guess there is something basic I'm missing out here :~ /matthias

      I love deadlines. I like the whooshing sound they make as they fly by.
      [Douglas Adams]

      D Offline
      D Offline
      dratcha
      wrote on last edited by
      #2

      * matches 0 or more occurences of the previous character or subexpression, while + matches 1 or more. Try this instead: const string sRegEx = "[a-z]+[A-Z]+[0-9]+";

      M 1 Reply Last reply
      0
      • D dratcha

        * matches 0 or more occurences of the previous character or subexpression, while + matches 1 or more. Try this instead: const string sRegEx = "[a-z]+[A-Z]+[0-9]+";

        M Offline
        M Offline
        matthias s 0
        wrote on last edited by
        #3

        Thanks for your reply! dratcha wrote: <* matches 0 or more occurences of the previous character or subexpression, while + matches 1 or more. Yes, but if I use a + instead of a * doesn't that mean i will definetely need 1 char in the range [a-z], plus one in the range [A-Z] and so forth? With the expression I've build in my previous post (using *), I thought to provide a subset of characters which should be allowed as input. But the string '-- --' marched right through claiming it was valid. How come? It wasn't listed in any of the subsets! I just want to understand... :^) /matthias

        I love deadlines. I like the whooshing sound they make as they fly by.
        [Douglas Adams]

        L 1 Reply Last reply
        0
        • M matthias s 0

          Thanks for your reply! dratcha wrote: <* matches 0 or more occurences of the previous character or subexpression, while + matches 1 or more. Yes, but if I use a + instead of a * doesn't that mean i will definetely need 1 char in the range [a-z], plus one in the range [A-Z] and so forth? With the expression I've build in my previous post (using *), I thought to provide a subset of characters which should be allowed as input. But the string '-- --' marched right through claiming it was valid. How come? It wasn't listed in any of the subsets! I just want to understand... :^) /matthias

          I love deadlines. I like the whooshing sound they make as they fly by.
          [Douglas Adams]

          L Offline
          L Offline
          leppie
          wrote on last edited by
          #4

          matthias s. wrote: But the string '-- --' marched right through claiming it was valid. How come? DId you look at the actual text that was matched? No. Else you would see the match length wasnt 0, in your case it will be. xacc-ide 0.0.15 now with C#, MSIL, C, XML, ASP.NET, Nemerle, MyXaml and HLSL coloring - Screenshots

          M 1 Reply Last reply
          0
          • M matthias s 0

            Why does this match?

            string sSearch = "-- --";
            const string sRegEx = "[a-z]*[A-Z]*[0-9]*";
            if (Regex.IsMatch(sSearch, sRegEx)) {
            // do something funny
            }

            My understanding is, it should allow any number of characters in the range of a-z, A-Z or any number between 0-9. I guess there is something basic I'm missing out here :~ /matthias

            I love deadlines. I like the whooshing sound they make as they fly by.
            [Douglas Adams]

            L Offline
            L Offline
            leppie
            wrote on last edited by
            #5

            [a-z]*[A-Z]*[0-9]* will match: aA0 aA a A0 a0 aaaaaAAAA0000, etc. I think you are looking for: [a-zA-Z0-9]+ Note the 'match one or more'. xacc-ide 0.0.15 now with C#, MSIL, C, XML, ASP.NET, Nemerle, MyXaml and HLSL coloring - Screenshots

            1 Reply Last reply
            0
            • L leppie

              matthias s. wrote: But the string '-- --' marched right through claiming it was valid. How come? DId you look at the actual text that was matched? No. Else you would see the match length wasnt 0, in your case it will be. xacc-ide 0.0.15 now with C#, MSIL, C, XML, ASP.NET, Nemerle, MyXaml and HLSL coloring - Screenshots

              M Offline
              M Offline
              matthias s 0
              wrote on last edited by
              #6

              Hi leppie, thanks for your reply. I still don't understand why IsMatch is returning true. For my understanding it should return true if a match has been found. My regular Expression states, that allowed characters are a-z, A-Z, 0-9 in any quantity. It doesn't say that '--' is a valid character. :doh: Could you please provide a short snippet that does the following: Check a given string (say, the SearchString) whether all input characters are falling into these categories a-z, A-Z, 0-9. If the SearchString contains one or more invalid characters, such as : or ; or -, the check should fail and an empty string should be returned. If all characters are valid, the snippet should return the complete SearchString as it was originally passed to the method. I usually don't ask people writing code for me, but I really would like to understand the workings here. Drives me nuts to walk around and make a stupid face... X| Thanks in advance! /matthias

              I love deadlines. I like the whooshing sound they make as they fly by.
              [Douglas Adams]

              L U 2 Replies Last reply
              0
              • M matthias s 0

                Hi leppie, thanks for your reply. I still don't understand why IsMatch is returning true. For my understanding it should return true if a match has been found. My regular Expression states, that allowed characters are a-z, A-Z, 0-9 in any quantity. It doesn't say that '--' is a valid character. :doh: Could you please provide a short snippet that does the following: Check a given string (say, the SearchString) whether all input characters are falling into these categories a-z, A-Z, 0-9. If the SearchString contains one or more invalid characters, such as : or ; or -, the check should fail and an empty string should be returned. If all characters are valid, the snippet should return the complete SearchString as it was originally passed to the method. I usually don't ask people writing code for me, but I really would like to understand the workings here. Drives me nuts to walk around and make a stupid face... X| Thanks in advance! /matthias

                I love deadlines. I like the whooshing sound they make as they fly by.
                [Douglas Adams]

                L Offline
                L Offline
                leppie
                wrote on last edited by
                #7

                matthias s. wrote: allowed characters are a-z, A-Z, 0-9 in any quantity In Regex terms thats is an ambigious description. Do you mean? a. any number of a-z followed by, any number of A-Z followed by, any number of 0-9 ([a-z]*[A-Z]*[0-9]*). This is also note really correct (it can be, but the Regex wil be complex), but you can check the Length property of the returned Match object if its larger than 0. b. one of more of a-z OR A-Z OR 0-9 ([a-zA-Z0-9]+ or using a character class \w+ that matches any alphabet char or number). Also you want to check the whole input string, so u will need to add SOL/EOL markers. Eg. ^\w+$ xacc-ide 0.0.15 now with C#, MSIL, C, XML, ASP.NET, Nemerle, MyXaml and HLSL coloring - Screenshots

                1 Reply Last reply
                0
                • M matthias s 0

                  Hi leppie, thanks for your reply. I still don't understand why IsMatch is returning true. For my understanding it should return true if a match has been found. My regular Expression states, that allowed characters are a-z, A-Z, 0-9 in any quantity. It doesn't say that '--' is a valid character. :doh: Could you please provide a short snippet that does the following: Check a given string (say, the SearchString) whether all input characters are falling into these categories a-z, A-Z, 0-9. If the SearchString contains one or more invalid characters, such as : or ; or -, the check should fail and an empty string should be returned. If all characters are valid, the snippet should return the complete SearchString as it was originally passed to the method. I usually don't ask people writing code for me, but I really would like to understand the workings here. Drives me nuts to walk around and make a stupid face... X| Thanks in advance! /matthias

                  I love deadlines. I like the whooshing sound they make as they fly by.
                  [Douglas Adams]

                  U Offline
                  U Offline
                  User 1635202
                  wrote on last edited by
                  #8

                  if u want enter string not contains any special chars. It should contain only alpha numeric then use this code string sSearch = "-- --";const string sRegEx = "[a-zA-Z0-9]"; if (Regex.IsMatch(sSearch, sRegEx)) //if return true { // do something funny } Naveen Sagar

                  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