簡體   English   中英

創建正則表達式匹配數組

[英]Create array of regex matches

在 Java 中,我試圖將所有正則表達式匹配項返回到一個數組,但似乎您只能檢查該模式是否匹配某些內容(布爾值)。

如何使用正則表達式匹配來形成與給定字符串中的正則表達式匹配的所有字符串的數組?

(如果您可以假設 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());
 }

在此之后, 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<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(); }
      };
    }
  };
}

有了這個,

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

在 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 使正則表達式過於復雜,並且不遵循 perl 風格。 看看MentaRegex ,看看如何在一行 Java 代碼中完成它:

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

這是一個簡單的例子:

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

(如果你有更多的捕獲組,你可以通過它們的索引作為 group 方法的參數來引用它們。如果你需要一個數組,那么使用list.toArray()

來自官方 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<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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM