match regular expression

Welcome Forums General PowerShell Q&A match regular expression

This topic contains 4 replies, has 3 voices, and was last updated by

bin
 
Participant
2 months, 2 weeks ago.

  • Author
    Posts
  • #164731
    bin

    Participant
    Topics: 5
    Replies: 4
    Points: 29
    Rank: Member

    hello

    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 

  • #164749

    Participant
    Topics: 0
    Replies: 44
    Points: 235
    Helping Hand
    Rank: Participant

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

  • #164767
    bin

    Participant
    Topics: 5
    Replies: 4
    Points: 29
    Rank: Member

    thank you

  • #164770

    Participant
    Topics: 2
    Replies: 54
    Points: 278
    Helping Hand
    Rank: Contributor

    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:

    \d{5}\W?-?\d?

    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:

    \d{5}

    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...
  • #164797
    bin

    Participant
    Topics: 5
    Replies: 4
    Points: 29
    Rank: Member

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

You must be logged in to reply to this topic.