|
Looks like you're specifying the pattern for valid values, rather than invalid values.
Try:
^([^6]|6[^0]|60[^0]) Regexper[^]
regex101: build, test, and debug regex[^]
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Why do you need a regex for such a simple test?
if (text.substring(0, 3).equals("600"))
...
|
|
|
|
|
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.
-- modified 12-Jun-22 10:58am.
|
|
|
|
|
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!
|
|
|
|