简体   繁体   English

使用正则表达式从Java中的匹配字符串中提取组

[英]Extracting a group from matched String in Java using regex

I have a list of String containing values like this: 我有一个包含这样的值的字符串列表:

String [] arr = {"${US.IDX_CA}", "${UK.IDX_IO}", "${NZ.IDX_BO}", "${JP.IDX_TK}", "${US.IDX_MT}", "more-elements-with-completely-different-patterns-which-is-irrelevant"};

I'm trying to extract all the IDX_XX from this list. 我正在尝试从此列表中提取所有IDX_XX。 So from above list, i should have, IDX_CA, IDX_IO, IDX_BO etc using regex in Java 所以从上面的列表中,我应该在Java中使用正则表达式来拥有IDX_CA,IDX_IO,IDX_BO等

I wrote following code: 我写了以下代码:

Pattern pattern = Pattern.compile("(.*)IDX_(\\w{2})");
for (String s : arr){
     Matcher m = pattern.matcher(s);
      if (m.matches()){
        String extract = m.group(1);
        System.out.println(extract);
      }
}

But this does not print anything. 但这不会打印任何内容。 Can someone please tell me what mistake am i making. 有人可以告诉我我在犯什么错误。 Thanks. 谢谢。

Use the following fix: 使用以下修复程序:

String [] arr = {"${US.IDX_CA}", "${UK.IDX_IO}", "${NZ.IDX_BO}", "${JP.IDX_TK}", "${US.IDX_MT}", "more-elements-with-completely-different-patterns-which-is-irrelevant"};
Pattern pattern = Pattern.compile("\\bIDX_(\\w{2})\\b");
for (String s : arr){
     Matcher m = pattern.matcher(s);
      while (m.find()){
        System.out.println(m.group(0)); // Get the whole match
        System.out.println(m.group(1)); // Get the 2 chars after IDX_
      }
}

See the Java demo , output: 参见Java演示 ,输出:

IDX_CA
CA
IDX_IO
IO
IDX_BO
BO
IDX_TK
TK
IDX_MT
MT

NOTES : 注意事项

  • Use \\bIDX_(\\w{2})\\b pattern that matches IDX_ and 2 word chars in between word boundaries and captures the 2 chars after IDX_ into Group 1 使用\\bIDX_(\\w{2})\\b模式,该模式匹配IDX_和两个单词字符之间的字字符,并在IDX_之后IDX_到第1组中的2个字符
  • m.matches needs a full string match, so it is replaced with m.find() m.matches需要完整的字符串匹配,因此将其替换为m.find()
  • if replaced with while in case there are more than 1 match in a string ifwhile替换,以防在字符串中有多个匹配项
  • m.group(0) contains the whole match values m.group(0)包含整个匹配值
  • m.group(1) contains the Group 1 values. m.group(1)包含第1组的值。

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

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