RegEx simple

Hey

I'm looking for the RegEx for the following:

All numbers from 0-10
All numbers from 0x-10x (where x is the letters a-z - and only one letter)

Thanks in advance

Mike (Using PowerShell v2)
LVL 1
mikeydkAsked:
Who is Participating?
 
Chris DentPowerShell DeveloperCommented:
heh one more modification. You can make the letter on the end optional. Doing so will make it match:

0 - 9
10
0a - 9z
10a - 10z

Version 3:

^(\d|10)[a-z]?$

Chris
0
 
point_pleasantCommented:
All numbers from 0-10
\d
All numbers from 0x-10x (where x is the letters a-z - and only one letter
\d[a-z]
0
 
Bryan ButlerCommented:
In case you need to combine them:

\d[a-z]?

where \d is digits, [a-z] is any lowercase letter, and ? is 0 or 1 occurances.
0
On-Demand: Securing Your Wi-Fi for Summer Travel

Traveling this summer?Check out our on-demand webinar to learn about the importance of Wi-Fi security and 3 easy measures you can start taking immediately to protect your private data while using public Wi-Fi. Follow us today to learn more!

 
mikeydkAuthor Commented:
I would like to use one Regex...

All numbers from 0-10
\d (gives match all numbers, not only 0-10)
All numbers from 0x-10x (where x is the letters a-z - and only one letter
\d[a-z](gives match for ex. 9aaa)
0
 
point_pleasantCommented:
ok try

\b[0-10][a-z]?\b

0
 
Bryan ButlerCommented:
\[0-9]{1}|[0-9]{1}[a-z]{1}



0
 
mikeydkAuthor Commented:
developedtester>

10 does not match (should be OK)
12a match (should not be OK)
0
 
mikeydkAuthor Commented:
point_pleasant> I'm unable to make this work?
0
 
Chris DentPowerShell DeveloperCommented:
Remember that \d cannot match "10" because that'd be two digit characters, not one.

You could do this:

\d0?

But it'll match 0 to 9, then 00, 10, 20, etc.

You could use Or to get it to match 0 - 9 or 10:

\d|10

Then the character on the end of each:

(\d|10)[a-z]

Finally, position in the string is important with Match, because if you have this:

0001a

You'll get a match too. If you want to match at the beginning of the string, tell it:

^(\d|10)[a-z]

Test script:
1..20 | ForEach-Object { "$($_)a : " + ("$($_)a" -Match '^(\d|10)[a-z]') }

Open in new window

HTH

Chris
0
 
Chris DentPowerShell DeveloperCommented:
One more, depending on what you're matching against, you may want to tell it about the end of line as well.

^(\d|10)[a-z]$

Then you'll match this:

1a

But not these:

1aa
1a1
10a1

The tweaking can go on, but context is important.

Chris
0
 
mikeydkAuthor Commented:
Chris-Dent>

Thanks.. ;)

Thinks this will work: ^(\d|10)[a-z]$|^(\d|10)$
0
 
Chris DentPowerShell DeveloperCommented:
Yes, but see version 3 for a better optional letter :)

Chris
0
 
mikeydkAuthor Commented:
Yes ;) Thanks again :)
0
 
Chris DentPowerShell DeveloperCommented:
You're welcome :)

Chris
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.