Pattern Check Match
-
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
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 ?
-
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 ?
"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!
-
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
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]
-
"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!
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.
-
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
Peter, I was too hasty, Here is what works best: '[A-Za-z]'+'[\.]' + '[A-Za-z]'+'[A-Za-z0-9]?[0-9]'
-
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
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
-
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
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
-
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
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......
-
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......
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
-
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.
why not this? ^[A-z]+\.[A-z]+\d{1,2}$