首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在匹配前提取所有单词?

如何在匹配前提取所有单词?
EN

Stack Overflow用户
提问于 2020-03-12 11:29:37
回答 1查看 43关注 0票数 2

是否有任何正则表达式将titleaddress从文本分离到下面的输出?

,到目前为止,这就是我所拥有的:

代码语言:javascript
复制
.+?(?=\d+.*Singapore \d{6}\b)

文本:

代码语言:javascript
复制
Marina Bay Sands Relocated! 2 Bayfront Avenue Galleria Level #B1-01 Singapore 018972
+65 6634 9969
nex 23 Serangoon Central #B1-10 Singapore 556083
+65 6634 7787
Northpoint City 1 Northpoint Drive South Wing #B1-107 Singapore 768019
+65 6481 3433

输出:

代码语言:javascript
复制
Marina Bay Sands Relocated! 
2 Bayfront Avenue Galleria Level #B1-01 Singapore 018972

nex 
23 Serangoon Central #B1-10 Singapore 556083

Northpoint City 1 Northpoint Drive South Wing #B1-107 Singapore 768019
+65 6481 3433
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-03-12 12:01:03

你可以用

代码语言:javascript
复制
(.+?)\s*(\d+.*Singapore \d{6})\b(?:\r?\n(\+65\s*\d{4}\s*\d{4}))?

或者只是

代码语言:javascript
复制
(.+?)\s*(\d+.*Singapore \d{6})\b(?:\r?\n(\+65[\d ]*))?

regex演示

详细信息

  • (.+?) -第1组:除换行字符以外的任何1或更多字符,尽可能少
  • \s* - 0+白空间
  • (\d+.*Singapore \d{6}) -第2组: 1+数字,除行中断字符以外的任何0+字符,尽可能多的Singapore,然后是6位数字。
  • \b -字边界
  • (?:\r?\n(\+65\s*\d{4}\s*\d{4}))? -一个可选的序列
    • \r?\n - CRLF或LF线结束
    • (\+65\s*\d{4}\s*\d{4}) -第3组:+65,0+白空间,4位数字,0+空格,4位数字。[\d ]*将匹配0或更多位数或空格。

每场比赛有三组内容:

代码语言:javascript
复制
Marina Bay Sands Relocated!
2 Bayfront Avenue Galleria Level #B1-01 Singapore 018972
+65 6634 9969

nex
23 Serangoon Central #B1-10 Singapore 556083
+65 6634 7787

Northpoint City
1 Northpoint Drive South Wing #B1-107 Singapore 768019
+65 6481 3433
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60653238

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档