Java模式在`ABC`中找到两组两个字母 [英] Java pattern to find two groups of two letters in `ABC`

查看:113
本文介绍了Java模式在`ABC`中找到两组两个字母的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样定义的模式:

I have a pattern defined like this:

 private static final Pattern PATTERN = Pattern.compile("[a-zA-Z]{2}");

在我的代码中,我这样做:

And in my code I'm doing this:

 Matcher matcher = PATTERN.matcher(myString);

并使用while循环查找所有匹配项。

and using a while loop to find all matches.

 while (matcher.find()){
 //do something here
 }

如果 myString 12345AB3CD45 匹配器正在找到那些两组两个字母( AB CD )。问题是我有时 myString 12345ABC356 所以我希望匹配器找到,首先 AB 然后 BC (只找到'AB)。

If myString is 12345AB3CD45 the matcher is finding those two groups of two letters (AB and CD). The problem is that I have sometimes myString as 12345ABC356 so I would like the matcher to find, first AB and then BC (is only finding `AB).

Am我做错了或正则表达式错误或匹配器不能正常工作?

Am I doing this wrong or the regex is wrong or the matcher doesn't work this way?

推荐答案

放置的文本片段在组0中(整场比赛)不能在下一场比赛中重复使用,成为组0的一部分。

Fragment of text which was placed in group 0 (entire match) can't be reused in next match to be part of group 0.

12345ABC356
     ^^  - AB was placed in standard match (group 0)
      ^^ - B can't be reused here as part of standard match

您可以通过诸如预见,它不会消费匹配的部分(它们是零长度),但您可以将它们的内容放在单独的捕获组,您将可以访问它。

You can solve this problem with look-around mechanisms like look-ahead, which doesn't consume matched part (they are zero-length), but you can place their content in separate capturing group which you will be able to access.

所以您的代码可以看起来喜欢

So your code can look like

private static final Pattern PATTERN = Pattern.compile("[a-zA-Z](?=([a-zA-Z]))");
//                                                      ^^^^^^^^   ^^^^^^^^^^
//                                                       group 0    group 1

//...

Matcher matcher = PATTERN.matcher(myString);
while (matcher.find()){
    String match = matcher.group() + matcher.group(1);
    //...
}

这篇关于Java模式在`ABC`中找到两组两个字母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆