Search code examples
iosregexunicodephone-numberios-shortcut

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


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?


Solution

  • 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.