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

  • SEARCH
  • Home
  • 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 955359
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T00:22:08+00:00 2026-05-16T00:22:08+00:00

I have a string that i am trying to extract patterns from, the string

  • 0

I have a string that i am trying to extract patterns from, the string is as follows:

(  ELT2N ( ELTOK wpSA910 wpSA909 wpSA908 wpSA474 ) )

The problem is, i dont know how many of the strings beginning with ‘wp’ will be in the string i am trying to search, however i want toi extract all of them using one statement. I am currently using the pattern below:

private final static String STARS_LINE_PATTERN = "\\(\\s+?(\\w+?)\\s+?\\(\\s+(\\w+)\\s+?(\\w+?\\s??){1,}\\s+?\\)\\s+?\\)";

The pattern is matching the string and returning the ‘ELT2N’ and the ‘ELTOK’ strings but is not returning the strings prefixed by ‘wp’.

Can anyone help?

Thanks

Simon

  • 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-16T00:22:08+00:00Added an answer on May 16, 2026 at 12:22 am

    Java regex like most flavors can only keep the last capture when you repeat a capturing group.

    For this particular problem, you may want to match the entire wp sequence into one group in one regex, and then post-process it again with another regex. In this case, a simple split is enough.

    Here’s a snippet to illustrate the idea:

        import java.util.regex.*;
        import java.util.*;
        //...
    
        String text = "(  ELT2N ( ELTOK wpSA910 wpSA909 wpSA908 wpSA474 ) )";
        String regex =
            "< (word) < (word) ((?:word )+)> >"
                .replace(" ", "\\s+")
                .replace("<", "\\(")
                .replace(">", "\\)")
                .replace("word", "\\w+");
    
        Matcher m = Pattern.compile(regex).matcher(text);
        if (m.find()) {
            System.out.printf("%s; %s;%n%s",
                m.group(1),
                m.group(2),
                Arrays.toString(m.group(3).split("\\s+"))
            );
        }
    

    The above prints:

    ELT2N; ELTOK;
    [wpSA910, wpSA909, wpSA908, wpSA474]
    

    So the entire wp sequence is captured by \3 of the regex pattern, which is then split into its parts.

    References

    • regular-expressions.info/Repeating a Capturing Group vs Capturing a Repeating Group

    Related questions

    • Is there a regex flavor that allows me to count the number of repetitions matched by * and +?
      • In .NET, you can query all intermediate Captures, but not so in Java
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying I have a string that I pass into my function from user
I have a string value that I'm trying to extract list items for. I'd
I am trying to match on a string that begins with ./media I have
Have been trying to encrypt an xml file to a string so that I
I have a web method that returns a String[][] and I'm trying to parse
I have a hashmap that contains multiple string arrays. I am trying to output
I have string that look like Array that fetched from other webservice like this
I am trying to extract all substrings in a string that is between the
I'm trying to extract CSS positions from a string - example: top left bottom
I'm trying to extract the names from this list: new String: blood cz TheDeAtH

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.