Remove +1 and parentheses from phone number

This topic contains 3 replies, has 3 voices, and was last updated by  Wilfredo Perez 1 year, 2 months ago.

  • Author
    Posts
  • #54611

    Wilfredo Perez
    Participant

    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

    Matt Bloomfield
    Participant

    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

      Olaf Soyk
      Participant

      Another solution could be this:

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

    Wilfredo Perez
    Participant

    Thanks Matt it worked

You must be logged in to reply to this topic.