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 6655541
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:31:57+00:00 2026-05-26T01:31:57+00:00

Consider the following regex: (([^\|])*\|)*([^\|]*) This matches repetitive string patterns of the type (whatever

  • 0

Consider the following regex:

(([^\|])*\|)*([^\|]*)

This matches repetitive string patterns of the type

("whatever except |" |) {0 to any times} ("whatever except |" |) {1 time}

So it should match the following String, which has 17 substrings (16 repeated, plus ” z” as the last one).

"abcd  | e | fg | hijk | lmnop | |   | qrs |   t| uv| w |||||x   y|  z"

Indeed, RegexPal verifies that the given regex does match the above string.

Now, I want to get each of the substrings (i.e., “abcd |”, “e |”, “fg |”, etc.), for which there is no prior knowledge about their number, length etc.

According to a similarly-titled previous StackOverflow post and the documentation of the Matcher class find() method, I just need to do something like

Pattern pattern = Pattern.compile(regex); // regex is the above regex
Matcher matcher = pattern.matcher(input); // input is the above string

while (matcher.find())
{
   System.out.println(matcher.group(1));
}

However, when I do this I just get 2 strings printed out: the last repeated substring (“x y|”) and a null value; definitely not the 16 substrings I expect.

A nice thing would also be to check that a match has actually happened, before running the find() loop, but I am not sure whether matches(), groupCount() > 0, or some other condition should be used, without doing twice the matching work, given that find() also does matching.

So, questions:

  1. How can I get all the 16 repeated substrings?
  2. How can I get the last substring?
  3. How do I check that the string matched?
  • 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-26T01:31:58+00:00Added an answer on May 26, 2026 at 1:31 am

    If you must use the regular expression…

    1) How can I get all the 16 repeated substrings?

    See below. When cycling over for matches, you don’t need everything to match, just the section you want. (I get 17 matches–is this correct?)

    2) How can I get the last substring?

    Switching the delim to the start of the regex and also allowing ‘^’.

    3) How do I check that the string matched?

    What qualifies for a non-match? Any string will match.


    Here is a solution using regular expressions:

    String input = "abcd  | e | fg | hijk | lmnop | |   | qrs |   t| uv| w |||||x   y|  z";
    int expectedSize = 17;
    List<String> expected = new ArrayList<String>(Arrays.asList("abcd  ", " e ", " fg ", " hijk ", " lmnop ", " ", "   ", " qrs ", "   t", " uv", " w ", "",
        "", "", "", "x   y", "  z"));
    
    List<String> matches = new ArrayList<String>();
    
    // Pattern pattern = Pattern.compile("(?:\\||^)([^\\|]*)");
    Pattern pattern = Pattern.compile("(?:_?\\||^)([^\\|]*?)(?=_?\\||$)"); // Edit: allows _| or | as delim
    
    for (Matcher matcher = pattern.matcher(input); matcher.find();)
    {
      matches.add(matcher.group(1));
    }
    
    for (int idx = 0, len = matches.size(); idx < len; idx++)
    {
      System.out.format("[%-2d] \"%s\"%n", idx + 1, matches.get(idx));
    }
    
    assertSame(expectedSize, matches.size());
    assertEquals(expected, matches);
    

    Output

    [1 ] "abcd  "
    [2 ] " e "
    [3 ] " fg "
    [4 ] " hijk "
    [5 ] " lmnop "
    [6 ] " "
    [7 ] "   "
    [8 ] " qrs "
    [9 ] "   t"
    [10] " uv"
    [11] " w "
    [12] ""
    [13] ""
    [14] ""
    [15] ""
    [16] "x   y"
    [17] "  z"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following: string keywords = (load|save|close); Regex x = new Regex(@\b+keywords+\b); I get
consider the following two pieces of code: public static Time Parse(string value) { string
While using some regex in C#, I'm facing the following problem : Consider this
consider following scenario input string = WIPR.NS i have to replace this with WIPR2.NS
Let's consider the two following lines in C# (using framework .NET 3.5) Regex regex
Consider following example : public class SomeBusinessLayerService : DataService<MyEntityContainer> { [WebInvoke] void DoSomething(string someParam)
Consider following program: static void Main (string[] args) { int i; uint ui; i
Consider the following Pattern :- aba And the foll. source string :- abababbbaba 01234567890
Consider the following tweets: RT @username This is my tweet Check this! RT @username
Consider the following string: No, @Username, I did not go to the supermarket yet

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.