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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T13:48:28+00:00 2026-06-09T13:48:28+00:00

I have the following pattern definition: private static final String D = (Mon|Tue|Wed|Thu|Fri|Sat|Sun); private

  • 0

I have the following pattern definition:

private static final String D = "(Mon|Tue|Wed|Thu|Fri|Sat|Sun)";
private static final String DD = "(" + D + "-" + D + ")";
private static final String m_daysPattern = "(" + D + "|" + DD + ")(?:,(" + D + "|" + DD + "))*";
private static final Pattern m_daysRegex = Pattern.compile(m_daysPattern);

This regex should match expressions like these:

  • “Mon-Fri”
  • “Tue-Fri,Sun”
  • “Mon-Wed,Fri-Sun”

I did it pretty easily in .NET using named groups, but here in Java 6 I am a bit lost. Can anyone show me an example of a code, which would allow me to extract the days and understand exactly which ranges and single days are mentioned in the given expression?

Thanks.

EDIT1

OK, here is what I get examining the groups of m_daysRegex.matcher("Mon-Fri"):

matchDays.groupCount() = 10
matchDays.groups = [0,7,0,7,-1,-1,0,7,0,3,4,7,-1,...,-1] (22 values)
matchDays.group(0) = "Mon-Fri"
matchDays.group(1) = "Mon-Fri"
matchDays.group(2) = null
matchDays.group(3) = "Mon-Fri"
matchDays.group(4) = "Mon"
matchDays.group(5) = "Fri"
matchDays.group(6) = null
matchDays.group(7) = null
matchDays.group(8) = null
matchDays.group(9) = null
matchDays.group(10) = null

Can someone explain to me the logic of all this? I mean not only should I get “Mon” and “Fri”, but I also have to know that they are part of a range subexpression “Mon-Fri”, rather than just “Mon,Fri”.

BTW, matching “Mon,Fri” gives us this:

matchDays.groupCount() = 10
matchDays.groups = [0,7,0,3,0,3,-1,-1,-1,-1,-1,-1,4,7,4,7,-1,...,-1] (22 values)
matchDays.group(0) = "Mon,Fri"
matchDays.group(1) = "Mon"
matchDays.group(2) = "Mon"
matchDays.group(3) = null
matchDays.group(4) = null
matchDays.group(5) = null
matchDays.group(6) = "Fri"
matchDays.group(7) = "Fri"
matchDays.group(8) = null
matchDays.group(9) = null
matchDays.group(10) = null

It is different, which is good. Still I find it hard to understand the algorithm.

This is quite easy in .NET, so I was expecting something as easy in Java. Fair, no?

EDIT2

Is there a Java regex guide explaining this stuff? All the guides I have seen so far examine really simple regular expressions.

EDIT3

OK, I begin to get it. My regex can be depicted like this:

 (          // 1 = D1|D2-D3
  (D1)|     // 2 = D1
  (         // 3 = D2-D3
   (D2)-    // 4 = D2
   (D3)     // 5 = D3
  )
 )
 (?:,
  (         // 6 = D4|D5-D6
   (D4)|    // 7 = D4
   (        // 8 = D5-D6
    (D5)-   // 9 = D5
    (D6)    // 10 = D6
   )
  )
 )*

It explains the group values for “Mon-Fri” and “Mon,Fri”. But how are the repetitions handled? For instance, “Mon,Wed,Fri” ? Still trying to figure it out.

EDIT4

I now get it. I can match a complex regex with repetitions, but I cannot extract the respective matches easily – must iterate using the find() method and a simpler subregex. So, in my case I have decided to:

  1. Match the entire expression to make sure it is valid
  2. Split it (with StringUtils.split from apache) by ‘,’
  3. Iterate the split parts, splitting by ‘-‘ if necessary.

Thanks to all the good Samaritans out there for all the help.

  • 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-09T13:48:29+00:00Added an answer on June 9, 2026 at 1:48 pm

    For a description of capturing groups etc, go here: http://docs.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html

    As for your pattern, if you write out all parantheses you get:

    ((d)|((d)-(d)))(?:,((d)|((d)-(d))))*
    

    where d is

    Mon|Tue|...
    

    Listing the groups, using M to show which parts that belong to a given group:

     0 - the matching string, if there is one
     1 - ((M)|((M)-(M)))(?:,((d)|((d)-(d))))*
     2 - ((M)|((d)-(d)))(?:,((d)|((d)-(d))))*
     3 - ((d)|((M)-(M)))(?:,((d)|((d)-(d))))*
     4 - ((d)|((M)-(d)))(?:,((d)|((d)-(d))))*
     5 - ((d)|((d)-(M)))(?:,((d)|((d)-(d))))*
     6 - ((d)|((d)-(d)))(?:,((M)|((M)-(M))))*
     7 - ((d)|((d)-(d)))(?:,((M)|((d)-(d))))*
     8 - ((d)|((d)-(d)))(?:,((d)|((M)-(M))))*
     9 - ((d)|((d)-(d)))(?:,((d)|((M)-(d))))*
    10 - ((d)|((d)-(d)))(?:,((d)|((d)-(M))))*
    

    Given the string “Mon-Fri” you’ll get the strings you see in the output, i.e.

     0 - Mon-Fri i.e. the full match
     1 - Mon-Fri i.e. ((M)|((M)-(M)))
     2 - null i.e. not matched by (M)
     3 - Mon-Fri i.e. ((M)-(M))
     4 - Mon i.e. the first (M) in group 3
     5 - Fri i.e. the second (M) in group 3
     6 - and so on...
    

    For multiple matches you’ll do something like

    Matcher m = pattern.matcher(input);
    while (m.find()) {
        // extract groups from m
    }
    

    EDIT: come to think of it, you probably don’t need the last part, i.e.

    (?:,((d)|((d)-(d))))*
    

    in your pattern. Just use the while loop with the

    (d)|((d)-(d))
    

    pattern to find each part (i.e. the stuff between the commas).

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

Sidebar

Related Questions

I have the following method: protected BigDecimal stringToBigDecimal(String value) throws ParseException { if (Pattern.matches([\\d,.]+,
I currently have the following Get-ChildItem -recurse -include file.ext | Select-String -pattern c: |
I have the following pattern: (COMPANY) -277.9887 (ASP,) -277.9887 (INC.) I want the final
I have the following string pattern: 1:2,2:3 . This is like array in one
I have the following pattern of a string: METHOD URL VERSION example GET /someurl/resource.html
I have the following pattern: Jan(COMPANY) &^% Feb(ASP) 567 Mar(INC) I want the final
I have the following pattern matching case in a scala function: def someFunction(sequences: Iterable[Seq[Int]]):Seq[Int]
I have the following pattern: template <int a, int b> class MyClass { public:
I have the following Repository Pattern. Requirement is to Find All accounts whose owner’s
I have the following Model pattern: public abstract class PARENTCLASS {...} public class CHILD_A_CLASS

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.