match regular expression


i need to match an expression for ID could be 88903 5 digit or 99892A 5 digit with letter [A-G] or 88987-1 5 disgit with letter dash [1-5] with confition IF

like this

if (IDentity -match “(^\d{5}[A-G]$)” -or “(^\d{5}[A-G]-[1-5]$)”)) is my syntax correct ?

thank you

you can try like this if (IDentity -match “(^\d{5}[A-G]$) | (^\d{5}[A-G]-[1-5]$)”)

thank you

If I understand correctly, you have three formats to match:

  1. 12345
  2. 12345A
  3. 12345A-1
Unless you specifically need to exclude letters beyond G, and/or numbers larger than 5 after the dash, you can match all of those with 1 regex string:
You can avoid any OR statements.

But also, if all of your ID numbers have the initial 5 digits, and you don’t need to exclude ID numbers with more than 5 digits, the only part you really need is this:


This will match every string that contains 5 numbers in a row, which will still include your values that have extra stuff on the end.

What does the rest of the ID number data set look like? What do you need to NOT match?

Also, if IDentity is a variable that contains the numbers you want to match, you need to call it with a $ like this:

if ($IDentity -match "(regex)") {etc...

excellent, this is what i need , thank you very much