Java 创建正则表达式匹配数组

Java 创建正则表达式匹配数组,第1张

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;        }                public void remove() { throw new UnsupportedOperationException(); }      };    }  };}

有了这个,

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

产量

a at 0b at 1a at 3c at 4a at 5a at 7b at 8a at 10


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5011290.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-15
下一篇 2022-11-14

发表评论

登录后才能评论

评论列表(0条)

保存