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. Regular Expressions
  4. Pattern Check Match

Pattern Check Match

Scheduled Pinned Locked Moved Regular Expressions
regexwindows-adminhelptutoriallearning
13 Posts 4 Posters 37 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
    Member_15670831
    wrote on last edited by
    #1

    Friends, I am using PowerShell. I am trying to write a regex match pattern for a string that has ALL these qualifications: 1. Must have one and only one period in the string. A. At least one Alpha, A-Za-z before the period. B. At least one Alpha, A-Z Za-z after the period. 2. The last character in the string must be numeric, 0-9 No Alpha, A-Za-z 3. The second-to-the-last character must be one of these two: A. May be Alpha, , A-Z Za-z or B. May be numeric, 0-9 4. No Numerics, 0-9 Allowed except the last two characters in the string. Examples of good ones: Joe.Jones1 Joe.Jones01 J.J1 j.j99 jo.jn01 jo.j1 I have written many small pieces of code that could be pieced together using -AND for many matches but surely there is a way to make some one-liner that can incorporate all of these. I may be able to do it using -AND like this: $test -notmatch “[0-9]” -AND $test -NOTmatch “[\.]" -AND $test -match “[a-zA-Z]" -AND $test -NOTmatch “[@#$%^&*()]" But I would like to be eloquent and do something like this: $GoodPatternCheck = $PossibleGoodName -match '.+,.+,.+,.+' If ( $GoodPatternCheck ) I have written many short matches in my learning process which I plan on sharing with my co-workers who are also novices. I don't see any way to attach a file here, so here is an example of the kinds of short piece of code I write and test to help me learn: $test = "Now......... is t.he, tim,e" Write-Host ‘$test’ $test $Count = ($test.Split('\.')).count -1 Write-Host ‘$test’ $test Write-Host ‘$Count’ $Count Thank you in advance for your help on this.

    P M 2 Replies Last reply
    0
    • M Member_15670831

      Friends, I am using PowerShell. I am trying to write a regex match pattern for a string that has ALL these qualifications: 1. Must have one and only one period in the string. A. At least one Alpha, A-Za-z before the period. B. At least one Alpha, A-Z Za-z after the period. 2. The last character in the string must be numeric, 0-9 No Alpha, A-Za-z 3. The second-to-the-last character must be one of these two: A. May be Alpha, , A-Z Za-z or B. May be numeric, 0-9 4. No Numerics, 0-9 Allowed except the last two characters in the string. Examples of good ones: Joe.Jones1 Joe.Jones01 J.J1 j.j99 jo.jn01 jo.j1 I have written many small pieces of code that could be pieced together using -AND for many matches but surely there is a way to make some one-liner that can incorporate all of these. I may be able to do it using -AND like this: $test -notmatch “[0-9]” -AND $test -NOTmatch “[\.]" -AND $test -match “[a-zA-Z]" -AND $test -NOTmatch “[@#$%^&*()]" But I would like to be eloquent and do something like this: $GoodPatternCheck = $PossibleGoodName -match '.+,.+,.+,.+' If ( $GoodPatternCheck ) I have written many short matches in my learning process which I plan on sharing with my co-workers who are also novices. I don't see any way to attach a file here, so here is an example of the kinds of short piece of code I write and test to help me learn: $test = "Now......... is t.he, tim,e" Write-Host ‘$test’ $test $Count = ($test.Split('\.')).count -1 Write-Host ‘$test’ $test Write-Host ‘$Count’ $Count Thank you in advance for your help on this.

      P Offline
      P Offline
      Peter_in_2780
      wrote on last edited by
      #2

      You may need to tweak bits of this to suit PowerShell's regex engine, but the idea should work.

      [A-Za-z]+\.[A-Za-z]+[A-Za-z0-9]?[0-9]

      In words: one or more alpha, a literal period, one or more alpha, an optional alphanumeric, one numeric.

      Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012

      M 5 Replies Last reply
      0
      • P Peter_in_2780

        You may need to tweak bits of this to suit PowerShell's regex engine, but the idea should work.

        [A-Za-z]+\.[A-Za-z]+[A-Za-z0-9]?[0-9]

        In words: one or more alpha, a literal period, one or more alpha, an optional alphanumeric, one numeric.

        Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012

        M Offline
        M Offline
        Member_15670831
        wrote on last edited by
        #3

        Peter, you must be some kind of genius! I thought it would take days for somebody to figure this out. Here is what works on my PowerShell ISE: '[A-Za-z]'+'[\.]' + '[A-Za-z]+[A-Za-z0-9]?[0-9]' Perfect! Thanks! Now I will go study what the question mark means ?

        OriginalGriffO 1 Reply Last reply
        0
        • P Peter_in_2780

          You may need to tweak bits of this to suit PowerShell's regex engine, but the idea should work.

          [A-Za-z]+\.[A-Za-z]+[A-Za-z0-9]?[0-9]

          In words: one or more alpha, a literal period, one or more alpha, an optional alphanumeric, one numeric.

          Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012

          M Offline
          M Offline
          Member_15670831
          wrote on last edited by
          #4

          Peter, you must be some kind of genius! I thought it would take days for somebody to figure this out. Here is what works on my PowerShell ISE: '[A-Za-z]'+'[\.]' + '[A-Za-z]+[A-Za-z0-9]?[0-9]' Perfect! Thanks! Now I will go study what the question mark means ?

          1 Reply Last reply
          0
          • M Member_15670831

            Peter, you must be some kind of genius! I thought it would take days for somebody to figure this out. Here is what works on my PowerShell ISE: '[A-Za-z]'+'[\.]' + '[A-Za-z]+[A-Za-z0-9]?[0-9]' Perfect! Thanks! Now I will go study what the question mark means ?

            OriginalGriffO Offline
            OriginalGriffO Offline
            OriginalGriff
            wrote on last edited by
            #5

            "As few as possible" Is you are going to play with Regular Expressions, then you need a tool. Get a copy of Expresso[^] - it's free, and it examines and generates Regular expressions.

            "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!

            "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
            "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

            M 1 Reply Last reply
            0
            • P Peter_in_2780

              You may need to tweak bits of this to suit PowerShell's regex engine, but the idea should work.

              [A-Za-z]+\.[A-Za-z]+[A-Za-z0-9]?[0-9]

              In words: one or more alpha, a literal period, one or more alpha, an optional alphanumeric, one numeric.

              Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012

              M Offline
              M Offline
              Member_15670831
              wrote on last edited by
              #6

              Peter, I was too hasty again. The problem is: This is supposed to match: Joe.Jone2 Not supposed to match: Joe.Jone222 I am using: [A-Za-z]'+'[\.]' + '[A-Za-z]+[A-Za-z0-9]?[0-9] Matches Joe.Jone2 Also Matches Joe.Jones22 Prolem is, it also matches Joe.Jones222 Only the last two may be numeric. Examples of good ones: Joe.Jones1 Joe.Jones01 J.J1 j.j99 jo.jn01 jo.j1 Examples of ones that I do not want to match, Bad ones: Joe.Jones222 Joe2.Jones1 Joe.2Jones01 I am testing/playing with: [A-Za-z]'+'[\.]' + '[A-Za-z]'+'[A-Za-z0-9]?[0-9]

              1 Reply Last reply
              0
              • OriginalGriffO OriginalGriff

                "As few as possible" Is you are going to play with Regular Expressions, then you need a tool. Get a copy of Expresso[^] - it's free, and it examines and generates Regular expressions.

                "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!

                M Offline
                M Offline
                Member_15670831
                wrote on last edited by
                #7

                OriginalGriff, Thanks for the information, I will look at that! Yes, I think I need a tool. I just found Regex101.com also. I don't know how these work yet, I hope I can get these to work.

                1 Reply Last reply
                0
                • P Peter_in_2780

                  You may need to tweak bits of this to suit PowerShell's regex engine, but the idea should work.

                  [A-Za-z]+\.[A-Za-z]+[A-Za-z0-9]?[0-9]

                  In words: one or more alpha, a literal period, one or more alpha, an optional alphanumeric, one numeric.

                  Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012

                  M Offline
                  M Offline
                  Member_15670831
                  wrote on last edited by
                  #8

                  Peter, I was too hasty, Here is what works best: '[A-Za-z]'+'[\.]' + '[A-Za-z]'+'[A-Za-z0-9]?[0-9]'

                  1 Reply Last reply
                  0
                  • P Peter_in_2780

                    You may need to tweak bits of this to suit PowerShell's regex engine, but the idea should work.

                    [A-Za-z]+\.[A-Za-z]+[A-Za-z0-9]?[0-9]

                    In words: one or more alpha, a literal period, one or more alpha, an optional alphanumeric, one numeric.

                    Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012

                    M Offline
                    M Offline
                    Member_15670831
                    wrote on last edited by
                    #9

                    Peter, I am having better luck by adding two Dollar Signs $ at the end. The last Test below here is the only problem now. I want J.j1 to be true too. Here are the tests: PS C:\Users\gmsab> "j.jo1" -match '[A-Za-z]'+'[\.]' + '[A-Za-z]+[A-Za-z0-9]$?[0-9$]$' True PS C:\Users\gmsab> "joe.jones11" -match '[A-Za-z]'+'[\.]' + '[A-Za-z]+[A-Za-z0-9]$?[0-9$]$' True PS C:\Users\gmsab> "j.jones11" -match '[A-Za-z]'+'[\.]' + '[A-Za-z]+[A-Za-z0-9]$?[0-9$]$' True PS C:\Users\gmsab> "j.jones1" -match '[A-Za-z]'+'[\.]' + '[A-Za-z]+[A-Za-z0-9]$?[0-9$]$' True PS C:\Users\gmsab> "j.jones" -match '[A-Za-z]'+'[\.]' + '[A-Za-z]+[A-Za-z0-9]$?[0-9$]$' False PS C:\Users\gmsab> "j.jones221" -match '[A-Za-z]'+'[\.]' + '[A-Za-z]+[A-Za-z0-9]$?[0-9$]$' False PS C:\Users\gmsab> "j.j21" -match '[A-Za-z]'+'[\.]' + '[A-Za-z]+[A-Za-z0-9]$?[0-9$]$' True PS C:\Users\gmsab> "j.j1" -match '[A-Za-z]'+'[\.]' + '[A-Za-z]+[A-Za-z0-9]$?[0-9$]$' False

                    P 1 Reply Last reply
                    0
                    • M Member_15670831

                      Peter, I am having better luck by adding two Dollar Signs $ at the end. The last Test below here is the only problem now. I want J.j1 to be true too. Here are the tests: PS C:\Users\gmsab> "j.jo1" -match '[A-Za-z]'+'[\.]' + '[A-Za-z]+[A-Za-z0-9]$?[0-9$]$' True PS C:\Users\gmsab> "joe.jones11" -match '[A-Za-z]'+'[\.]' + '[A-Za-z]+[A-Za-z0-9]$?[0-9$]$' True PS C:\Users\gmsab> "j.jones11" -match '[A-Za-z]'+'[\.]' + '[A-Za-z]+[A-Za-z0-9]$?[0-9$]$' True PS C:\Users\gmsab> "j.jones1" -match '[A-Za-z]'+'[\.]' + '[A-Za-z]+[A-Za-z0-9]$?[0-9$]$' True PS C:\Users\gmsab> "j.jones" -match '[A-Za-z]'+'[\.]' + '[A-Za-z]+[A-Za-z0-9]$?[0-9$]$' False PS C:\Users\gmsab> "j.jones221" -match '[A-Za-z]'+'[\.]' + '[A-Za-z]+[A-Za-z0-9]$?[0-9$]$' False PS C:\Users\gmsab> "j.j21" -match '[A-Za-z]'+'[\.]' + '[A-Za-z]+[A-Za-z0-9]$?[0-9$]$' True PS C:\Users\gmsab> "j.j1" -match '[A-Za-z]'+'[\.]' + '[A-Za-z]+[A-Za-z0-9]$?[0-9$]$' False

                      P Offline
                      P Offline
                      Peter_in_2780
                      wrote on last edited by
                      #10

                      Too many $. In all the regex engines I use (I don't do PowerShell), $ matches the end of the input string. I forgot that off my original answer, sorry. It should be there to disallow trailing garbage. Similarly you probably need a ^ at the start to match start of string. In regex, * means "0 or any number of occurrences of the previous item" + means "1 or more occurrences..." ? means "0 or 1 occurrences..." . matches any character, so to match a literal period, you need to escape it with \ So your regex should finally be ^[A-Za-z]+\.[A-Za-z]+[A-Za-z0-9]?[0-9]$ only adding whatever delimiters and escapes are required by PowerShell (which should probably be just ' ' around the whole exprerssion).

                      Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012

                      M 1 Reply Last reply
                      0
                      • P Peter_in_2780

                        Too many $. In all the regex engines I use (I don't do PowerShell), $ matches the end of the input string. I forgot that off my original answer, sorry. It should be there to disallow trailing garbage. Similarly you probably need a ^ at the start to match start of string. In regex, * means "0 or any number of occurrences of the previous item" + means "1 or more occurrences..." ? means "0 or 1 occurrences..." . matches any character, so to match a literal period, you need to escape it with \ So your regex should finally be ^[A-Za-z]+\.[A-Za-z]+[A-Za-z0-9]?[0-9]$ only adding whatever delimiters and escapes are required by PowerShell (which should probably be just ' ' around the whole exprerssion).

                        Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012

                        M Offline
                        M Offline
                        Member_15670831
                        wrote on last edited by
                        #11

                        Peter, I have tested this as you said: ^[A-Za-z]+\.[A-Za-z]+[A-Za-z0-9]?[0-9]$ I cannot make it fail with any of my inputs, good input and bad input, it returns True and False at all the right places. I will stress test it again tomorrow. And I will study all this too. Many Thanks......

                        P 1 Reply Last reply
                        0
                        • M Member_15670831

                          Peter, I have tested this as you said: ^[A-Za-z]+\.[A-Za-z]+[A-Za-z0-9]?[0-9]$ I cannot make it fail with any of my inputs, good input and bad input, it returns True and False at all the right places. I will stress test it again tomorrow. And I will study all this too. Many Thanks......

                          P Offline
                          P Offline
                          Peter_in_2780
                          wrote on last edited by
                          #12

                          You're welcome. And keep studying. Don't just stop because your immediate problem is solved.

                          Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012

                          1 Reply Last reply
                          0
                          • M Member_15670831

                            Friends, I am using PowerShell. I am trying to write a regex match pattern for a string that has ALL these qualifications: 1. Must have one and only one period in the string. A. At least one Alpha, A-Za-z before the period. B. At least one Alpha, A-Z Za-z after the period. 2. The last character in the string must be numeric, 0-9 No Alpha, A-Za-z 3. The second-to-the-last character must be one of these two: A. May be Alpha, , A-Z Za-z or B. May be numeric, 0-9 4. No Numerics, 0-9 Allowed except the last two characters in the string. Examples of good ones: Joe.Jones1 Joe.Jones01 J.J1 j.j99 jo.jn01 jo.j1 I have written many small pieces of code that could be pieced together using -AND for many matches but surely there is a way to make some one-liner that can incorporate all of these. I may be able to do it using -AND like this: $test -notmatch “[0-9]” -AND $test -NOTmatch “[\.]" -AND $test -match “[a-zA-Z]" -AND $test -NOTmatch “[@#$%^&*()]" But I would like to be eloquent and do something like this: $GoodPatternCheck = $PossibleGoodName -match '.+,.+,.+,.+' If ( $GoodPatternCheck ) I have written many short matches in my learning process which I plan on sharing with my co-workers who are also novices. I don't see any way to attach a file here, so here is an example of the kinds of short piece of code I write and test to help me learn: $test = "Now......... is t.he, tim,e" Write-Host ‘$test’ $test $Count = ($test.Split('\.')).count -1 Write-Host ‘$test’ $test Write-Host ‘$Count’ $Count Thank you in advance for your help on this.

                            M Offline
                            M Offline
                            Member 13115581
                            wrote on last edited by
                            #13

                            why not this? ^[A-z]+\.[A-z]+\d{1,2}$

                            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