java字符串包含多次,在Java中使用正则表达式多次匹配一个字符串

I'm having some issues with making the following regex work. I would like the following string:

"Please enter your name here"

to result in an array with the following elements:

'please enter', 'enter your', 'your name', 'name here'

Currently, I'm using the following pattern, and then creating a matcher and iterating in the following way:

Pattern word = Pattern.compile("[\w]+ [\w]+");

Matcher m = word.matcher("Please enter your name here");

while (m.find()) {

wordList.add(m.group());

}

But the result I'm getting is:

'please enter', 'your name'

What am I doing wrong? (P.s., i checked the same regex on regexpal.com and had the same problem). It seems like the same word won't be matched twice. What can I do to achieve the result I want?

Thanks.

---------------------------------

EDIT:

Thanks for all the suggestions! I ended up doing this (because it adds flexibility in being able to easily specify number of "n-grams"):

Integer nGrams = 2;

String patternTpl = "\\b[\\w']+\\b";

String concatString = "what is your age? please enter your name."

for (int i = 0; i < nGrams; i++) {

// Create pattern.

String pattern = patternTpl;

for (int j = 0; j < i; j++) {

pattern = pattern + " " + patternTpl;

}

pattern = "(?=(" + pattern + "))";

Pattern word = Pattern.compile(pattern);

Matcher m = word.matcher(concatString);

// Iterate over all words and populate wordList

while (m.find()) {

wordList.add(m.group(1));

}

}

This results in:

Pattern:

(?=(\b[\w']+\b)) // In the first iteration

(?=(\b[\w']+\b \b[\w']+\b)) // In the second iteration

Array:

[what, is, your, age, please, enter, your, name, what is, is your, your age, please enter, enter your, your name]

Note: Got the pattern from the following top answer: Java regex skipping matches

解决方案

The matches can't overlap, which explains your result. Here's a potential workaround, making use of capturing groups with a positive lookahead:

Pattern word = Pattern.compile("(\\w+)(?=(\\s\\w+))");

Matcher m = word.matcher("Please enter your name here");

while (m.find()) {

System.out.println(m.group(1) + m.group(2));

}

Please enter

enter your

your name

name here

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值