创建正则表达式匹配数组

Jak*_*key 143 java regex

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

Mik*_*uel 255

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

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

 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());
 }
Run Code Online (Sandbox Code Playgroud)

在此之后,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.
}
Run Code Online (Sandbox Code Playgroud)

做这样的事情:

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(); }
      };
    }
  };
}
Run Code Online (Sandbox Code Playgroud)

有了这个,

for (MatchResult match : allMatches(Pattern.compile("[abc]"), "abracadabra")) {
  System.out.println(match.group() + " at " + match.start());
}
Run Code Online (Sandbox Code Playgroud)

产量

a at 0
b at 1
a at 3
c at 4
a at 5
a at 7
b at 8
a at 10
Run Code Online (Sandbox Code Playgroud)

  • @Liv,花时间对'ArrayList`和`LinkedList`进行基准测试,结果可能会令人惊讶. (13认同)
  • @Liv,如果你的模式往往会产生匹配具有相当可预测的大小,取决于模式是否匹配疏或密(基于allMatches`的``VS yourStringHere.length()`长度的总和),你可以为`allMatches`预先计算一个好的大小.根据我的经验,"LinkedList"内存和迭代效率方面的成本通常不值得,所以`LinkedList`不是我的默认姿势.但是在优化热点时,绝对值得交换列表实现,看看你是否得到了改进. (12认同)
  • 我不建议在这里使用ArrayList,因为你不知道大小的前期,可能想避免缓冲区大小调整.相反,我更喜欢LinkedList - 虽然它只是一个建议,并没有使你的答案不那么有效. (4认同)
  • 在 Java 9 中,您现在可以使用 [`Matcher#results`](https://docs.oracle.com/javase/9​​/docs/api/java/util/regex/Matcher.html#results--) 来获取一个可用于生成数组的“Stream”(请参阅​​[我的答案](/sf/answers/3280139131/))。 (2认同)

4ca*_*tle 37

在Java中9,你现在可以用Matcher#results()得到Stream<MatchResult>,你可以用它来获得匹配的列表/阵列.

import java.util.regex.Pattern;
import java.util.regex.MatchResult;
Run Code Online (Sandbox Code Playgroud)
String[] matches = Pattern.compile("your regex here")
                          .matcher("string to search from here")
                          .results()
                          .map(MatchResult::group)
                          .toArray(String[]::new);
                    // or .collect(Collectors.toList())
Run Code Online (Sandbox Code Playgroud)

  • @Bravo您使用的是Java 9吗?它确实存在.我链接到文档. (8认同)
  • :(( java 8 有没有其他选择 (2认同)

Tra*_*ago 25

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

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

  • MentaRegex网站是否已关闭?当我访问http://mentaregex.soliveirajr.com/时,它只会说"嗨" (12认同)
  • 这很酷.双斜线看起来仍然很难看,但我猜这里没有任何景观. (6认同)
  • 注意!最好的解决方案.用它! (2认同)

Boz*_*zho 9

这是一个简单的例子:

Pattern pattern = Pattern.compile(regexPattern);
List<String> list = new ArrayList<String>();
Matcher m = pattern.matcher(input);
while (m.find()) {
    list.add(m.group());
}
Run Code Online (Sandbox Code Playgroud)

(如果你有更多的捕获组,你可以通过它们的索引引用它们作为组方法的参数.如果你需要一个数组,那么使用list.toArray())


Ant*_*oly 5

来自官方正则表达式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;
        }
Run Code Online (Sandbox Code Playgroud)

使用find并将结果插入group您的数组/列表/任何内容.