代码之家  ›  专栏  ›  技术社区  ›  SocketM

用于短语的Regex在Java altrough中未按预期运行,已在线测试[重复]

  •  1
  • SocketM  · 技术社区  · 6 年前

    在Java中,我试图将所有regex匹配返回给一个数组,但似乎只能检查模式是否匹配某个对象(布尔值)。

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

    0 回复  |  直到 5 年前
        1
  •  290
  •   castletheperson    6 年前

    ( 4castle's answer 如果您可以假设Java>=9,则比下面的更好)

     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
    
        2
  •  73
  •   castletheperson    7 年前

    在Java9中,现在可以使用 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())
    
        3
  •  25
  •   TraderJoeChicago    13 年前

    Java使得regex过于复杂,而且它不遵循perl风格。看一看 MentaRegex 要了解如何在一行Java代码中实现这一点,请执行以下操作:

    String[] matches = match("aa11bb22", "/(\\d+)/g" ); // => ["11", "22"]
    
        4
  •  12
  •   walkeros    7 年前

    下面是一个简单的例子:

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

        5
  •  5
  •   Anthony Accioly    13 年前

    Official 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

        6
  •  0
  •   Nikhil Kumar K    8 年前
            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;