We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

RegEx simple

Medium Priority
404 Views
Last Modified: 2012-05-11
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)
Comment
Watch Question

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]
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.

Author

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)
ok try

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

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



Author

Commented:
developedtester>

10 does not match (should be OK)
12a match (should not be OK)

Author

Commented:
point_pleasant> I'm unable to make this work?
Chris DentPowerShell Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
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
Chris DentPowerShell Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
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
PowerShell Developer
CERTIFIED EXPERT
Top Expert 2010
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
Chris-Dent>

Thanks.. ;)

Thinks this will work: ^(\d|10)[a-z]$|^(\d|10)$
Chris DentPowerShell Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
Yes, but see version 3 for a better optional letter :)

Chris

Author

Commented:
Yes ;) Thanks again :)
Chris DentPowerShell Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
You're welcome :)

Chris
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.