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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T07:38:38+00:00 2026-05-12T07:38:38+00:00

I want to parse data which might contain mixed patterns like 1-4pm 1pm-5pm noon

  • 0

I want to parse data which might contain mixed patterns like

1-4pm
1pm-5pm
noon to 11pm
noon to midnight
etc.

I want to extract start and end time. How can I achieve this through regex. I know I can’t support all possible input formats, but how can I achieve to support a maximum?


this is my expression

^((?<dayPart>[a-z]+)?)\s*(?<startTime>[0-9]{1,2}[:]?[0-9]{0,2}\s*[am|pm|a.m|p.m]*[.]*)?\s*[-|to|\\|/|=]*\s*((?<endPart>[a-z]+)?|(?<endTime>[0-9]{1,2}[:]?[0-9]{0,2}\s*[am|pm|a.m|p.m]*[.]*))?$

which covers almost all combination.

I just want to know if there is any optimization in this regex.

Here dayPart will consume all starting non-digit characters to handle if time-span starts with noon, midnight etc or any value which we can ignore like Sunday.
startTime will try to consume any time in any format if it is there. same is for endPart and endTime.

  • 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-12T07:38:38+00:00Added an answer on May 12, 2026 at 7:38 am

    First, define a pattern that matches a single point in time. Given your examples it might be something like:

    (noon|midnight|[0-9]+\s?(am|pm)?)
    

    Next, define the separator. Perhaps:

    (to|\-)
    

    Finally, combine two of the first with one of the second. Assuming your language supports variables, something like:

    set timePattern {(noon|midnight|[0-9]+\s?(am|pm)?)}
    set separator {(to|\-)}
    set fullPattern "$timePattern(\s*$separator\s*$timePattern)?"
    

    Once you pass that through the engine you should be able to get at the parts of the expression that matched. You might need to make some groups non-capturing but I’ll leave that as an exercise for the reader. You’ll then likely have to parse the individual parts to figure out the time. For example, parse “1pm” as a 1 and “pm” and calculate a time based on that.

    Once you have it broken down like that it makes it easier to fiddle with the constituent parts and makes the expression a bit more comprehensible. Though, the same thing can be accomplished in some languages that support multiline expressions with comments.

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

Sidebar

Related Questions

I have lines of data which I want to parse. The data looks like
Scenario I want to parse a large CSV file and inserts data into the
I want to get, via ajax, a collection of data objects and parse them
I want to parse a config file sorta thing, like so: [KEY:Value] [SUBKEY:SubValue] Now
I want to use RestKit to parse XML data from our servers. Downloading and
I want to parse data in between brackets. Below is the code where you
I am working on an android application in which I want to parse an
I've made a little forum and I want parse the date on newest posts
I want to parse some HTML in order to find the values of some
I want to parse a web page in Groovy and extract all of the

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.