regex - 用于选择带有至少两个下划线的单词的正则表达式
问题描述
我正在尝试解析包含一些带下划线的单词的文本文档。
我正在寻找正则表达式匹配,但目前失败了。
我正在查看获取(逐行)具有至少两个下划线的单词或具有至少两个下划线和正斜杠+至少三个数字的单词。
我已经到了
([a-zA-Z]+(?:_{2,}[a-zA-Z]+)*)
正确的匹配示例是
VOK17_05_530_526002 *(has atleast than two underscores)*
VIE_ROMS_002 *(has atleast than two underscores)*
VOK_OVSZ_001/002 *(has atleast two underscores and a forward slash + three digits)*
输入样本
VOK17_05_530_526002 502 504 BACU VIE_ROMS_002 VIE_ROMS_001 VOK_OVSZ_001/002
VOK17_05_530_526002 401 401 LGCU VIE_ROMS_002 VIE_ROMS_001 VOK_OVSZ_001/002
VOK17_05_530_526002 510 513 BACU VIE_ROMS_002 VIE_ROMS_001 VOK_OVSZ_001/002
VOK17_05_530_526002 515 515 BACU VIE_ROMS_002 VIE_ROMS_001 VOK_OVSZ_001/002
VOK17_05_530_526003 503 506 BACU VIE_ROMS_002 VIE_ROMS_001 VOK_OVSZ_001/002
我正在尝试我的正则表达式 @ https://regex101.com/r/yToVtc/1
如果有人可以在这里提供帮助,我将不胜感激。
解决方案
使用这个:
\b[a-zA-Z0-9]+(?:_[a-zA-Z0-9]+){2,}(?:/\d{3})?\b
解释:
\b # word boundary
[a-zA-Z0-9]+ # 1 or more alphanum
(?: # non capture group
_ # underscore
[a-zA-Z0-9]+ # 1 or more alphanum
){2,} # end group, must appear 2 or more times
(?: # non capture group
/ # a slask
\d{3} # 3 digits
)? # end group, optional
\b # word boundary
推荐阅读
- string - 在 SAS 中按全省名称查找和替换首字母缩略词
- sql - oracle中的XMLTABLE如何处理空列'
- android - 如何在Android中只显示一个片段选项卡
- ruby-on-rails - Heroku 上的部署失败 - Ruby on Rails
- asp.net-core - 布尔值渲染 asp-for 复选框,但它总是在 mvc 核心剃刀视图中得到真值而不是假?
- javascript - HTML 添加路径到子页面
- excel - excel中的VBA Vlookup“应用程序定义或对象定义”错误
- python - 如何从列表中创建字典,其中每个元素的计数是键,值是相应元素的列表?
- javascript - 更改了 src
- python - 如何从列表中减去列表中的列表中的值?