Remove +1 and parentheses from phone number

Welcome Forums General PowerShell Q&A Remove +1 and parentheses from phone number

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

 
Participant
2 years ago.

  • Author
    Posts
  • #54611

    Participant
    Points: 0
    Rank: Member

    The code works fine:

    $num = ($ADUser.TelephoneNumber).replace('+1 ', "")
    $num = $num.replace('(',"")
    $num = $num.replace(') ',"-")
    $num
    

    but I know that there is an easier way or I am doing something wrong. I am also fine using regex. I just need someone to help me

    From AD: +1 (212) ***-****
    I would like it this one: ***-***-****

  • #54615

    Participant
    Points: 0
    Rank: Member

    Not sure if this is the correct solution but it's a solution 🙂

    Just replace the right parenthesis and space with a dash and select the substring:

    $num.replace(') ','-').Substring(4)
    • #54619

      Participant
      Points: 9
      Rank: Member

      Another solution could be this:

      "+1 (212) 234-5678" -replace '\+1\s+\((\d{3})\)\s+(\d{3}-\d{4})','$1-$2'
  • #54620

    Participant
    Points: 0
    Rank: Member

    Thanks Matt it worked

The topic ‘Remove +1 and parentheses from phone number’ is closed to new replies.