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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T10:13:11+00:00 2026-06-01T10:13:11+00:00

Given a set of java regular expression patterns separated by an OR (i.e |

  • 0

Given a set of java regular expression patterns separated by an OR (i.e | ), is there any specific precedence that the patterns will follow.

Example code:-

    List<String> columnValues = new ArrayList<String>

    String []columnPatterns = new String[] { "(\\S\\s?)+", "(\\S\\s?)+",
                "(\\d+,?)+\\.\\d+ | \\d+:\\d+", "(\\S\\s?)+",
                "-?\\$?(\\d+,?)+\\.\\d+" };

    String searchString = "Text1            This is Text 2                                          129.80";

    int findFrom = 0;
    int columnIndex = 0;
    List<String> columnValues = new ArrayList<String>();
    for (String pattern : columnPatterns) {
        Pattern p = Pattern.compile(pattern);
        Matcher m = p.matcher(searchString);
        if (m.find(findFrom)) {
            columnValues.add(columnIndex++,
                    searchString.substring(m.start(), m.end()).trim());
            findFrom = m.end();
        }
    }

    for (String value : columnValues) {
        System.out.println("<" + value + ">");
    }

The above code yields the following result:-

    <Text1>
    <This is Text 2>
    <129.80>

But if I change the pattern at index position 2 in the columnPatterns array from “(\d+,?)+\.\d+ | \d+:\d+” to “(\d+,?)+\.\d+ | \d+:\d+ | \d+” as shown below:-

    columnPatterns = new String[] { "(\\S\\s?)+", "(\\S\\s?)+",
                "(\\d+,?)+\\.\\d+ | \\d+:\\d+ | \\d+", "(\\S\\s?)+",
                "-?\\$?(\\d+,?)+\\.\\d+" };

I get the following result:-

   <Text1>
   <This is Text 2>
   <129>
   <.80>

Does this mean there is some kind of implicit precedence getting applied or is there some other reason behind this and what could be a solution/work around for this behaviour?

Edit:
Also, why does the code behave the way it does.

  • 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-06-01T10:13:13+00:00Added an answer on June 1, 2026 at 10:13 am

    Given a set of java regular expression patterns separated by an OR (i.e | ), is there any specific precedence that the patterns will follow

    Left to right. At each position in the string each alternation will be tested in order, the one that matches first will be the final match (unless backtracked later).

    In your case the last alternation will match first because you have a space at the beginning of it, thus it can match when the previous alternations do not.

    For example matching the pattern \d| \d on the string foo 7, the second alternation will match first at the index 3 in the string. The first could not match at that position, and would only be able to match at index 4.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to generate a regular expression match in Java, that accepts all
In Java, given a pair of public methods, enableFoo and disableFoo, that set a
Is there a tool for Java which, given a set of JUnit tests, and
I can see that Collections.unmodifiableSet returns an unmodifiable view of the given set but
Is there an application , which can parse a given set of stored procedures
Scala doesn't have type-safe enum s like Java has. Given a set of related
I'm making a simple program in Java. Given a set of letters it'll list
Given a set of integers, how to find a subset that sums to a
I'm using Jtransforms java library to perform analysis on a given dataset. An example
Is there some one-liner bridge method to dump a given Enumeration to java.util.List or

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.