提取电话号码

Extracting telephone numbers

我想提取电话号码,但它只匹配 Place C 但不匹配 Place APlace B。有人可以帮我弄这个吗?提前致谢!

正则表达式

(.+?)\s*(\d+.*Singapore\s+\d{6}\b|\d+.*S\d{6})\b(?!(\.+?)\s*)(\+65[\d ]*)

已编辑文本

Place A
2 Bayfront Avenue Galleria Level #B1-01 Singapore 018972 Tel: +65 6634 9969

Place B
23 Serangoon Central #B1-10 Singapore 556083 Phone: 6634 7787

Place C
1 Northpoint Drive South Wing #B1-107 Singapore 768019 6481 3433

在6位数字和电话号码之间,可以有一个标签,所以你需要考虑到它并以某种方式消耗它。这样做的一个例子是允许任何字符有选择地存在,而不是 + 符号,像这样:

[^+]*

在您的正则表达式的上下文中,这使得它:

(.+?)\s*(\d+.*Singapore\s+\d{6}\b|\d+.*S\d{6})\b(?!(\.+?)\s*)[^+]*(\+65[\d ]*)

其中now matches所有三种情况。

编辑:

根据更新,我将正则表达式修改为 match all six cases:

(.+?)\s*(\d+.*?Singapore\s+\d{6}\b|\d+.*?S\d{6})\b[^+0-9]*((?:\+65)?[\d ]*)