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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T15:09:33+00:00 2026-06-14T15:09:33+00:00

I have a question about regexes in Java, though I think this might apply

  • 0

I have a question about regexes in Java, though I think this might apply to other languages as well.

I have a regex to parse time from a field where user may enter something like 4d 8h 42m. Of course, I want to make it as flexible as possible, so that user should not be obliged to type all numbers (and enter a mere 15h for instance).

My regex is quite satisfactory concerning that point: (?:([\d]+)d)?[\s]*(?:([\d]+)h)?[\s]*(?:([\d]+)m)?

Now my problem is that it will also match an empty string, though I would like it to ensure that at least one time-unit is filled.

The current solution would be to arbitrary choose one of them to be mandatory, but I am not satisfied with it since mandatory field is what I am trying to avoid.

Also, making an or does not suit me, since I would have to test groups when parsing the regex afterwards, instead of just accessing group(1) for days, group(2) for hours, …
(This is what I think of when speaking of an or : (?:([\d]+)d[\s]*(?:([\d]+)h)?[\s]*(?:([\d]+)m)?|(?:([\d]+)d)?[\s]*([\d]+)h[\s]*(?:([\d]+)m)?|(?:([\d]+)d)?[\s]*(?:([\d]+)h)?[\s]*([\d]+)m), to be understood as days mandatory or hours mandatory or minutes mandatory).

So how could I modify my regex to make sure that at least one of my now-non-capturing group is not empty, be it days, hours or minutes?

  • 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-14T15:09:34+00:00Added an answer on June 14, 2026 at 3:09 pm

    You can use a look-forward assert to ensure that at least one of d h or m appears.

    (?=.*[mhd])(?:(\d+)d)?\s*(?:(\d+)h)?\s*(?:(\d+)m)?
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a question about this formula from a book: EFW (cm,kg)= 10^(-1,7492+(0,166*BPD)+(0,046*AC)-(2,646*AC*BPD/1000)) The
I have a question about how GC works in Java. Consider the following code:
i have question about how to count matched elements in ArrayList in java.. Ex:
I have question about this plugin system: .NET 4.0 ASP.NET MVC 3 plug-in architecture
i have question about finding maximum x or y-value in text file. This is
Today I have question about Eclipse . I use this IDE very long and
I have question about try, catch and finally in Java. Consider the following scenario:
https://stackoverflow.com/a/64983/468251 - Hello, I have question about this code, how made that working with
I have question about signing java applets. I have self-signed applet. When i open
I have question about parsing in Html helper : I have sth like: @foreach

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.