java 正则 数组_创建正则表达式匹配数组

问题

在Java中,我试图将所有正则表达式匹配返回到数组,但似乎只能检查模式是否匹配(boolean)。有人可以帮我使用正则表达式匹配来形成一个匹配给定字符串中的正则表达式的所有字符串的数组吗?谢谢!

#1 热门回答(212 赞)

(4castle's answer如果可以假设Java> = 9,则优于以下内容)

你需要创建一个匹配器并使用它来迭代查找匹配项。

import java.util.regex.Matcher;

import java.util.regex.Pattern;

...

List allMatches = new ArrayList();

Matcher m = Pattern.compile("your regular expression here")

.matcher(yourStringHere);

while (m.find()) {

allMatches.add(m.group());

}

在此之后,allMatches包含匹配项,如果你确实需要,可以使用allMatches.toArray(new String[0])获取数组。

你也可以使用MatchResult来编写辅助函数来循环匹配,因为1990年至5061年会返回当前组状态的快照。

例如,你可以编写一个惰性迭代器来执行此操作

for (MatchResult match : allMatches(pattern, input)) {

// Use match, and maybe break without doing the work to find all possible matches.

}

做这样的事情:

public static Iterable allMatches(

final Pattern p, final CharSequence input) {

return new Iterable() {

public Iterator iterator() {

return new Iterator() {

// Use a matcher internally.

final Matcher matcher = p.matcher(input);

// Keep a match around that supports any interleaving of hasNext/next calls.

MatchResult pending;

public boolean hasNext() {

// Lazily fill pending, and avoid calling find() multiple times if the

// clients call hasNext() repeatedly before sampling via next().

if (pending == null && matcher.find()) {

pending = matcher.toMatchResult();

}

return pending != null;

}

public MatchResult next() {

// Fill pending if necessary (as when clients call next() without

// checking hasNext()), throw if not possible.

if (!hasNext()) { throw new NoSuchElementException(); }

// Consume pending so next call to hasNext() does a find().

MatchResult next = pending;

pending = null;

return next;

}

/**Required to satisfy the interface, but unsupported. */

public void remove() { throw new UnsupportedOperationException(); }

};

}

};

}

有了这个,

for (MatchResult match : allMatches(Pattern.compile("[abc]"), "abracadabra")) {

System.out.println(match.group() + " at " + match.start());

}

产量

a为0

b为1

a 3点

c在4点

a 5点

a 7点

b 8点

一点十分

#2 热门回答(25 赞)

Java使得regex过于复杂,并且它不遵循perl风格。查看MentaRegex,了解如何在一行Java代码中实现这一目标:

String[] matches = match("aa11bb22", "/(\\d+)/g" ); // => ["11", "22"]

#3 热门回答(15 赞)

在Java 9中,你现在可以使用Matcher#results()获取aStream,你可以使用它来获取匹配列表/数组。

import java.util.regex.Pattern;

import java.util.regex.MatchResult;

String[] matches = Pattern.compile("your regex here")

.matcher("string to search from here")

.results()

.map(MatchResult::group)

.toArray(String[]::new);

// or .collect(Collectors.toList())

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值