[英]Create array of regex matches
In Java, I am trying to return all regex matches to an array but it seems that you can only check whether the pattern matches something or not (boolean).在 Java 中,我试图将所有正则表达式匹配项返回到一个数组,但似乎您只能检查该模式是否匹配某些内容(布尔值)。
How can I use a regex match to form an array of all string matching a regex expression in a given string?如何使用正则表达式匹配来形成与给定字符串中的正则表达式匹配的所有字符串的数组?
( 4castle's answer is better than the below if you can assume Java >= 9) (如果您可以假设 Java >= 9,则4castle 的答案比下面的要好)
You need to create a matcher and use that to iteratively find matches.您需要创建一个匹配器并使用它来迭代查找匹配项。
import java.util.regex.Matcher;
import java.util.regex.Pattern;
...
List<String> allMatches = new ArrayList<String>();
Matcher m = Pattern.compile("your regular expression here")
.matcher(yourStringHere);
while (m.find()) {
allMatches.add(m.group());
}
After this, allMatches
contains the matches, and you can use allMatches.toArray(new String[0])
to get an array if you really need one.在此之后, allMatches
包含匹配项,如果您确实需要一个数组,您可以使用allMatches.toArray(new String[0])
来获取一个数组。
You can also use MatchResult
to write helper functions to loop over matches since Matcher.toMatchResult()
returns a snapshot of the current group state.您还可以使用MatchResult
编写辅助函数来循环匹配,因为Matcher.toMatchResult()
返回当前组状态的快照。
For example you can write a lazy iterator to let you do例如你可以写一个懒惰的迭代器来让你做
for (MatchResult match : allMatches(pattern, input)) {
// Use match, and maybe break without doing the work to find all possible matches.
}
by doing something like this:通过做这样的事情:
public static Iterable<MatchResult> allMatches(
final Pattern p, final CharSequence input) {
return new Iterable<MatchResult>() {
public Iterator<MatchResult> iterator() {
return new Iterator<MatchResult>() {
// 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(); }
};
}
};
}
With this,有了这个,
for (MatchResult match : allMatches(Pattern.compile("[abc]"), "abracadabra")) {
System.out.println(match.group() + " at " + match.start());
}
yields产量
a at 0 b at 1 a at 3 c at 4 a at 5 a at 7 b at 8 a at 10
In Java 9, you can now use Matcher#results()
to get a Stream<MatchResult>
which you can use to get a list/array of matches.在 Java 9 中,您现在可以使用Matcher#results()
来获取Stream<MatchResult>
,您可以使用它来获取匹配项列表/数组。
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())
Java makes regex too complicated and it does not follow the perl-style. Java 使正则表达式过于复杂,并且不遵循 perl 风格。 Take a look at MentaRegex to see how you can accomplish that in a single line of Java code:看看MentaRegex ,看看如何在一行 Java 代码中完成它:
String[] matches = match("aa11bb22", "/(\\d+)/g" ); // => ["11", "22"]
Here's a simple example:这是一个简单的例子:
Pattern pattern = Pattern.compile(regexPattern);
List<String> list = new ArrayList<String>();
Matcher m = pattern.matcher(input);
while (m.find()) {
list.add(m.group());
}
(if you have more capturing groups, you can refer to them by their index as an argument of the group method. If you need an array, then use list.toArray()
) (如果你有更多的捕获组,你可以通过它们的索引作为 group 方法的参数来引用它们。如果你需要一个数组,那么使用list.toArray()
)
From the Official Regex Java Trails :来自官方 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;
}
Use find
and insert the resulting group
at your array / List / whatever.使用find
并将结果group
插入到您的数组/列表/任何内容中。
Set<String> keyList = new HashSet();
Pattern regex = Pattern.compile("#\\{(.*?)\\}");
Matcher matcher = regex.matcher("Content goes here");
while(matcher.find()) {
keyList.add(matcher.group(1));
}
return keyList;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.