简体   繁体   中英

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, eg, +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?

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.

Alternative solution

It is possible to paste the copied clipboard text into a Text action which removes any additional formatting. iOS 14 快捷方式 - 使用“文本”操作删除格式

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM