Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5929129
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T14:22:52+00:00 2026-05-22T14:22:52+00:00

In Java, I am trying to return all regex matches to an array but

  • 0

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

How can I use a regex match to form an array of all string matching a regex expression in a given string?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T14:22:52+00:00Added an answer on May 22, 2026 at 2:22 pm

    (4castle’s answer is better than the below if you can assume Java >= 9)

    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.


    You can also use MatchResult to write helper functions to loop over matches
    since Matcher.toMatchResult() returns a snapshot of the current group state.

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to use back references in Java regex, but it seems I'm
What's up y'all, I am trying to write some code in Java that will
I'm trying to call SendMessage with an uint parameter from Java, but I can't
I am trying to write a general function in F# that would return all
Im trying to improve the Java Html Document a little but i'm running into
Im trying to consume a Java web-service, that uses a certificate. I could generate
First of all, I'm new to Java. I'm trying to figure out what would
Disclaimer: I'm completely new to Java EE/EJB and all that, so bear with me.
I'm trying to create a program that will return a certain number decomposed to
I'm trying to write a java programme which determines if all the elements of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.