Trying to use Regex match an iOS phone number using iOS Shortcuts

1k views Asked by At

I'm creating an iOS shortcut that starts with checking whether the clipboard currently holds a copied phone number.

So far, my regex is the following:

^[\d\()-‑+  ]+$

Although while typing a regular phone number using the phone keyboard, e.g., +972 12-345-6789, it works just fine; it seems like when copied directly from the default Phone application, it does not find it as a match.

I should mention that after digging for a while, it seems like the phone number copied from the Phone application is associated with some extra special characters used for Left-To-Right Embedding and such. Using a Unicode decoder gave me the following string:

‪+972 12‑345‑6789‬

I'm not even sure whether this is the reason, but it might have some connection to it. If so - is there any way in which I can use iOS Shortcuts to decode the clipboard text into a Unicode format and remove those extra characters? And in case there is no connection between the two, what else might be the problem?

3

There are 3 answers

0
Wiktor Stribiżew On BEST ANSWER

You can add the support for all these chars to the pattern:

^[\d()+\p{Zs}\p{Pd}\p{Cf}]+$`

Note:

  • \p{Zs} matches all whitespaces without the tabs and line breaks (add \t to the brackets if you need to support TAB)
  • \p{Pd} will match any dashes
  • \p{Cf} matches the bidirectional marks and more other control format chars.
0
Erelephant On

Alternative solution

It is possible to paste the copied clipboard text into a Text action which removes any additional formatting.iOS 14 shortcut- using 'Text' action to remove formatting

0
ProfessorE On

Alternative Solution

Shortcuts has a native command to extract phone numbers. Just use “Get Phone Numbers from _______” command in your shortcut. It recognizes all phone number formats.

Example: code block Shortcut example