正则表达式 - 从字符串中提取电话号码 [英] Regex - Extract phone numbers from string

查看:227
本文介绍了正则表达式 - 从字符串中提取电话号码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从带有分隔符(波浪号)的字符串中提取两个电话号码.棘手的部分是电话号码的格式可能会有所不同.

I need to extract two phone numbers from a string with delimiters (tilde). The tricky part is the format of the phone numbers can vary.

字符串模式保持不变.但是电话号码的格式可以是三种类型之一

The string pattern stays the same. But the formatting of the phone numbers can be one of three types

(1)  4 digit extensions. (ex. 1001)
(2) 10 digit (5551112222)
(3) 10 digit with country code (+15558889999)

我需要去掉 +1 国家/地区代码的分机号或 10 位数字.所以

I need the extension or 10 digit number with the +1 country code stripped off. So

(1) 1001 = 1001
(2) 5551112222 = 5551112222
(3) +15558889999 = 5558889999

示例字符串

2019/02/06/2019-02-06T084903~call~5551112222~+15558889999~231a6a62-c1c8-43a8-ac2e-f8428237385c.WAV

从上面的字符串中,我需要提取两个具有正确 10 位格式的电话号码

From the string above, I need to extract two phone numbers with correct 10 digit formatting

(1) 5551112222
(2) 5558889999

到目前为止,我有以下正则表达式:

So far, I have the following regex expression:

(?<=\~)(.*?)(?=\~)

这给了我三组

(1) Call
(2) 5551112222
(3) +15558889999

但是,我需要的是具有正确格式的两组

But, what I need is two groups with correct formatting

(1) 5551112222
(2) 5558889999

我将此正则表达式模式与 Integromat 一起使用,因此在这种情况下没有任何编码语言解决方案可用.它必须是 100% 正则表达式.

I'm using this regex pattern with Integromat so no coding language solutions will work in this case. It must be 100% regex.

感谢您对此的任何帮助.谢谢!

Appreciate any help on this. Thanks!

推荐答案

您可以使用

(?<=~\+|~)([0-9]+)(?=~)

查看正则表达式演示

如果lookbehind有问题,请使用稍微修改的变体:

If there is a problem with lookbehind, use a bit modified variant:

(?:(?<=~\+)|(?<=~))([0-9]+)(?=~)

详情

  • (?<=~\+|~) - 必须有 ~+~ 紧邻当前位置
  • ([0-9]+) - 第 1 组:一位或多位数字
  • (?=~) - 当前位置的右侧必须有 ~.
  • (?<=~\+|~) - there must be ~+ or ~ immediately to the left of the current location
  • ([0-9]+) - Group 1: one or more digits
  • (?=~) - there must be ~ immediately to the right of the current location.

这篇关于正则表达式 - 从字符串中提取电话号码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆