首页 > 解决方案 > 用于选择带有至少两个下划线的单词的正则表达式

问题描述

我正在尝试解析包含一些带下划线的单词的文本文档。

我正在寻找正则表达式匹配,但目前失败了。

我正在查看获取(逐行)具有至少两个下划线的单词或具有至少两个下划线和正斜杠+至少三个数字的单词。

我已经到了

([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

如果有人可以在这里提供帮助,我将不胜感激。

标签: regex

解决方案


使用这个:

\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

演示


推荐阅读