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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T18:27:14+00:00 2026-05-17T18:27:14+00:00

I have a problem with regex, using preg_match_all(), to match something of a variable

  • 0

I have a problem with regex, using preg_match_all(), to match something of a variable length.

What I am trying to match is the traffic condition after the word ‘Congestion’ What I came up with is this regex pattern:

Congestion\s*:\s*(?P<congestion>.*)

It would however, extract the first instance all the way to the end of the entire subject, since .* would match everything. But that’s not what I want though, I would like it to match separately as 3 instances.

Now since the words behind Congestion could be of variable length, I can’t really predict how many words and spaces are in between to come up with a stricter \w*\s*\w* match etc.

Any clues on how I can proceed from here?

Highway : Highway 26
Datetime : 18-Oct-2010 05:18 PM
Congestion : Traffic is slow from Smith St to Alice Springs St

Highway : Princes Highway
Datetime : 18-Oct-2010 05:18 PM
Congestion : Traffic is slow at the Flinders St / Elizabeth St intersection

Highway : Eastern Freeway
Datetime : 18-Oct-2010 05:19 PM
Congestion : Traffic is slow from Prince St to Queen St

EDIT FOR CLARITY

These very nicely formatted texts here, are actually received via a very poorly formatted html email. It contains random line breaks here and there eg “Congestion : Traffic\n is slow from Prince\nSt to Queen St”.

So while processing the emails, I stripped off all the html codes and the random line breaks, and json_encode() them into one very long single-line string with no line break…

  • 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-17T18:27:14+00:00Added an answer on May 17, 2026 at 6:27 pm

    You can try a minimal match:

    Congestion\s*:\s*(?P<congestion>.*?)

    This would result in returning zero characters in the named group ‘congestion’ unless you could match something immediately after the congestion string.

    So, this could be fixed if “Highway” always starts the traffic condition records:

    Congestion\s*:\s*(?P<congestion>.*?)Highway\s*:

    If this works (I have not checked it), then the first records are matched but the last record is not! This could be easily fixed by appending the text ‘Highway :’ at the end of the input string.

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

Sidebar

Related Questions

I'm using php's preg_replace function, and I have the following regex: (?:[^>(),]+) to match
I have problem with one of my validation regex when using nonstandard utf-8 character.
I'm trying to figure out a problem I'm having with regex. I'm using this
I have a problem with validating an email address. I am using a RegEx
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
I'm trying to get a regexp to solve the next problem: I have some
I have problem with my query on C, I’m using the oci8 driver. This

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.