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

The Archive Base Latest Questions

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

I have a problem with a regex pattern and optional matches. Basically I try

  • 0

I have a problem with a regex pattern and optional matches. Basically I try to extract info from a string containing working hours, that could be of the 3 following forms:

$d1 = 'Fr: 9-12;';
$d2 = 'Mo: 9-12 und 15-18; alle 14 Tage spez. Migräneberatung bis 20 Uhr;';
$d3 = 'Mo: 9-12; alle 14 Tage spez. Migräneberatung bis 20 Uhr;';

$regex = '
    /
        (Mo|Di|Mi|Do|Fr|Sa|So)+:          # day follow by colon
            \s+?                          # a optional space
        (\d+)\-(\d+)                      # time from - to
        (?:\s+?und\s+?(\d+)\-(\d+))       # optional time from - to
            ;                               
        (?:([^;]+))                       # optional addt info
    /x';

$rc = preg_match_all($regex, $d2, $m);

print_r($m);

The string $d2 is working without problem and I get all expected matches, but the strings $d1 and $d3 do not match. I tried the optional grouping with the 2nd time-part and the additional info text but it does not work. I get empty matches instead. I can not see the flaw …

I would like to use preg_match_all to get all occurrences of such above sub-string, because it is a large string for each day Monday to Sunday with above sub-strings in form of $d1 - $d3 per day. I do not know if I can also use the semicolon as this sub-string end-marker, this is why I try to match it with ([^;]+).
If this is not working I could choose another delimiter that marks the end of one day sub-string and just split the large string first and match the sub-strings in a loop.

I appreciate any hints! Thanks in advance for 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-14T00:23:10+00:00Added an answer on June 14, 2026 at 12:23 am

    I may be wrong here, but this seems to work:

    $regex = '
        /
            (Mo|Di|Mi|Do|Fr|Sa|So):           # day follow by colon
                \s+?                          # a optional space
            (\d+)\-(\d+)                      # time from - to
            (?:\s+?und\s+?(\d+)\-(\d+))?      # optional time from - to
                ;
            (?:([^;]+))?                      # optional addt info
        /x';
    

    Just added the optionality (question marks) for optional elements.

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

Sidebar

Related Questions

Hi I have a problem with my regex pattern: preg_match_all('/!!\d{3}/', '!!333!!333 !!333 test', $result);
I have a little problem on RegEx pattern in c#. Here's the rule below:
I have a Regex pattern that matches data I need to parse exactly as
Possible Duplicate: Java - Regex problem I have list of URLs of types: http://www.example.com/pk/etc
I have a problem with a difficult regex. I have this expression to detect
I have a problem with regards to writing the correct regex for the examination
While doing some small regex task I came upon this problem. I have a
Here my problem: @Assert\Regex( * pattern=/^[A-Za-z0-9][A-Za-z0-9\]*$/, * groups={creation, creation_logged} * ) I'm using the
Could you please help me to provide a RegEx pattern to validate a string
To summarize: How can I prevent my regex pattern from mistaking segments of strings

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.