|
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 ?
|
|
|
|
|
"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.
|
|
|
|
|
Peter, I was too hasty, Here is what works best:
'[A-Za-z]'+'[\.]' + '[A-Za-z]'+'[A-Za-z0-9]?[0-9]'
|
|
|
|
|
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]
|
|
|
|
|
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
|
|
|
|
|
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
|
|
|
|
|
why not this?
^[A-z]+\.[A-z]+\d{1,2}$
|
|
|
|
|
Hello all,
I have a question regarding regex address capture.
I have the following address:
Harald Sturm
Schönstrasse 18a
60311 Frankfurt am Main
I want to split this string into 4 components with 4 different regex to do that.
Name: Harald Sturm. (The whole first line)
Street: Schönstrasse 18a (The whole second line)
ZIP CODE: 60311 (First digital part of the sentence )
Location: Frankfurt am Main (Last part of the sentence)
Question for the regex experts: What would each regex expression for this look like ?
Many thanks in advance.
|
|
|
|
|
|
I don't have a label like name, street etc- I just always have three lines.
The first line always records the full name.
The second line records the street including the house number.
The third line consists of a postal code and city. Thereby
the postal code and the city should be extracted separately.
I would need four single regex rules at last.
Thanks a lot in advance
|
|
|
|
|
Did you try the regex I supplied? At all?
"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!
|
|
|
|
|
A few lines of code including one call to String.Split would do that much quicker than a regex.
|
|
|
|
|
You may right, but im using it for a rpa tool, that accept only a regex confirm formula for extracting operations.
|
|
|
|
|
hi!
I'm trying to write regex to get the parent DN. it looks like that:
$rx=[regex]'^CN=.*,((?:(?:OU|CN=).*,)*DC=domain,DC=name)$'
that part which is not working is of course '
((?:(?:OU|CN=).*,)* ' - i don't know how to define all parts that can be 'OU=whatever,' or 'CN=whatever,'
help plz ):
|
|
|
|
|
|
thx but i need independent code... and better understand regex. otherwise i could just use substring function, but I want to understand why my thinking doesn't work
|
|
|
|
|
I am looking to find out how to use letters for wildcard patterns.
Ex: 58596Q5555
Ex: 58596q5555
Q=0-4
q=5-9
I need to add many route patterns utilizing this method and am not sure how to add them. I was not able to find any info relating to this.
|
|
|
|
|
Scenario :
RITM1413432","state":"Open"},{"u_categor>\n","active":"true","request_item":"RITM1413419","state":"Open"},{"u_category"
I have to count how many times that ( "state":"Open" ) has in the paragraph by Regular expression.
Can somebody guide me?
Thanks!
|
|
|
|
|
Looks like a job for JSON[^], not a regex.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Why does e.*?r match eer, rather than er, in the word eerie?
My (clearly erroneous) logic is that .*? matches zero or more of any character and as few as possible, so er (with zero of any character) should be the match, not eer (with one of any character).
|
|
|
|
|
Since the .* pattern says match any char any number of times, any text between an 'e' and an 'r' match. In this case the pattern ? is redundant, since the preceding .* will already have matched everything between a literal 'e' and a literal 'r'. So the pattern is equivalent to e.*r
Keep Calm and Carry On
|
|
|
|