group() 数组java_关于Java:创建正则表达式匹配数组

在Java中,我试图将所有正则表达式匹配项返回到一个数组,但是似乎您只能检查该模式是否匹配某些内容(布尔值)。

如何使用正则表达式匹配在给定字符串中形成所有与正则表达式匹配的字符串数组?

好问题。 您寻求的信息应该是Regex和Matcher上的Java文档的一部分。 可悲的是,它不是。

真可惜。 几乎所有其他语言(具有正则表达式支持)似乎都可以立即使用此功能。

(如果可以假定Java> = 9,那么4castle的答案比下面的要好)

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

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编写帮助函数来循环匹配

因为Matcher.toMatchResult()返回当前组状态的快照。

例如,您可以编写一个惰性迭代器来完成

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 at 0

b at 1

a at 3

c at 4

a at 5

a at 7

b at 8

a at 10

我不建议在这里使用ArrayList,因为您不预先知道大小,并且可能想避免调整缓冲区大小。相反,我希望有一个LinkedList -尽管它只是一个建议,并且不会使您的答案无效。

@Liv,花时间对ArrayList和LinkedList进行基准测试,结果可能令人惊讶。

我听见你在说什么,我知道两种情况下的执行速度和内存占用量; ArrayList的问题是默认构造函数创建的容量为10-如果通过调用add()超过了该大小,则将不得不承担内存分配和数组复制的工作-这可能会发生几次。当然,如果您只希望进行几次比赛,那么您的方法会更有效;但是,如果您发现数组"调整大小"不止一次发生,我建议使用LinkedList,如果您处理的是低延迟应用程序,则更是如此。

@Liv,如果您的模式倾向于生成具有相当可预测的大小的匹配项,并且取决于模式是稀疏匹配还是密集匹配(基于allMatches与yourStringHere.length()的长度之和),则可以预先计算a allMatches的合适大小。以我的经验,LinkedList的内存成本和迭代效率通常不值得,因此LinkedList并不是我的默认姿势。但是在优化热点时,绝对值得交换列表实现以查看是否有所改进。

在Java 9中,您现在可以使用Matcher#results来获取Stream,可以将其用于生成数组(请参阅我的答案)。

但是ArrayList的大小调整非常有效。当您必须插入/删除节点时,最好使用LinkedList。我在这里看不到问题。另外,如果您认为10还不够,则只需估算并执行Listresults = new ArrayList<>(100);

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

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())

他们不是results()方法,请先运行

@Bravo您正在使用Java 9吗?它确实存在。我链接到文档。

Java使正则表达式过于复杂,并且不遵循perl样式。看一下MentaRegex,看看如何在一行Java代码中完成该任务:

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

这很酷。双斜杠看起来仍然很丑陋,但我想这没有任何可替代之处。

mentaregex-0.9.5.jar,6Kb拯救了我的生活,Obrigado Srgio!

注意!最好的解决方案。用它!

MentaRegex网站是否关闭?当我访问mentaregex.soliveirajr.com时,只会说"嗨"

@ user64141看起来像是

这是一个简单的例子:

Pattern pattern = Pattern.compile(regexPattern);

List list = new ArrayList();

Matcher m = pattern.matcher(input);

while (m.find()) {

list.add(m.group());

}

(如果您有更多捕获组,则可以通过它们的索引将它们作为组方法的参数来引用。如果需要数组,请使用list.toArray())

pattern.matches(input)不起作用。您必须再次传递您的正则表达式模式-> WTF Java? pattern.matches(字符串正则表达式,字符串输入);你是说pattern.matcher(input)吗?

@ElMac Pattern.matches()是静态方法,不应在Pattern实例上调用它。 Pattern.matches(regex, input)只是Pattern.compile(regex).matcher(input).matches()的简写。

从官方Regex Java Trails:

Pattern pattern =

Pattern.compile(console.readLine("%nEnter your regex:"));

Matcher matcher =

pattern.matcher(console.readLine("Enter input string to search:"));

boolean found = false;

while (matcher.find()) {

console.format("I found the text "%s" starting at" +

"index %d and ending at index %d.%n",

matcher.group(), matcher.start(), matcher.end());

found = true;

}

使用find并将生成的group插入到数组/列表/任何位置。

Set keyList = new HashSet();

Pattern regex = Pattern.compile("#\\{(.*?)\\}");

Matcher matcher = regex.matcher("Content goes here");

while(matcher.find()) {

keyList.add(matcher.group(1));

}

return keyList;

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值