match regular expression

Welcome Forums General PowerShell Q&A match regular expression

  • This topic has 4 replies, 3 voices, and was last updated 12 months ago by
    bin
    Participant
    .
Viewing 4 reply threads
  • 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: 45
      Points: 243
      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
      Senior Moderator
      Topics: 3
      Replies: 123
      Points: 653
      Helping Hand
      Rank: Major 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

Viewing 4 reply threads
  • The topic ‘match regular expression’ is closed to new replies.