简体   繁体   English

正则表达式未分组

[英]Regular Expression not grouping

Need help with this regex 需要此正则表达式的帮助

ABC          130  zlis               02-03/12 N180 Grouping req
A B Csd       130  pain          02/12 I80 alias

(\w+\s{0,3})(\d+)

The regex does not seem to group as I need it to. 正则表达式似乎并没有按照我的需要分组。

Desired Output, brackests are the groups im trying to detect. 期望的输出,最粗暴的是我试图检测的组。

(AB Csd) (130) (pain) (02/12) (I80) (alias) (AB Csd)(130)(疼痛)(02/12)(I80)(别名)

Try this regex: 试试这个正则表达式:

([az ]+?)\\s+(\\d+)\\s+([az]+)\\s+([\\d-\\/]+)\\s+([\\w ]+)

Click for Demo 点击演示

Explanation: 说明:

  • ([az ]+?) - match 1+ occurrences(as few as possible) of a letter or a space and capture it as Group1 ([az ]+?) -匹配字母或空格的1+次出现(尽可能少)并将其捕获为Group1
  • \\s+ - matches 1+ occurrences of a whitespace character \\s+ -匹配1+次出现的空白字符
  • (\\d+) - match 1+ occurrences of digits and capture as Group2 (\\d+) -匹配1+个数字出现并捕获为Group2
  • \\s+ - matches 1+ occurrences of a whitespace character \\s+ -匹配1+次出现的空白字符
  • ([az]+) - match 1+ occurrences of a letter and Capture as Group 3 ([az]+) -匹配1个以上的字母并将其捕获为第3组
  • \\s+ - matches 1+ occurrences of a whitespace character \\s+ -匹配1+次出现的空白字符
  • ([\\d-\\/]+) - match 1+ occurrences of a digit or - or / and capture it as Group4 ([\\d-\\/]+) -匹配1+次出现的数字或-/并将其捕获为Group4
  • \\s+ - matches 1+ occurrences of a whitespace character \\s+ -匹配1+次出现的空白字符
  • ([\\w ]+) - match 1+ occurrences of a word-character or a space and capture as Group5 ([\\w ]+) -匹配1个以上的字符或空格并将其捕获为Group5

Note that I have used the g , i , m flags for Global matches, Case-insensitive and Multiline respectively. 请注意,我分别对全局匹配,不区分大小写和多行使用了gim标志。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM