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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T15:01:20+00:00 2026-05-22T15:01:20+00:00

how do we match a string against multiple patters in PCRE ? I need

  • 0

how do we match a string against multiple patters in PCRE ? I need to match this string

subj1 = "9112345678 put car details of the car";

patt1 = "(\\d+) ([a-z]+) ([a-z]+) (.+)";

some times the subject can be like this

subj2 = "9112345678 put car";

which is matching pattern

patt2 = "(\\d+) ([a-z]+) ([a-z]+)";

since the subject is dynamic and not known a prior would like to “or” these 2 patterns and want to match the subject against a composite pattern.
some thing like
sub matching (patt1 or patt2)
can we do this in PCRE ?

  • 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-22T15:01:20+00:00Added an answer on May 22, 2026 at 3:01 pm

    You just need to make the last chunk optional:

    (\\d+) ([a-z]+) ([a-z]+)( (.+))?
    

    And if you also need to allow “9112345678 put” then add more optional groups:

    (\\d+) ([a-z]+)( ([a-z]+)( (.+))?)?
    

    If you want to maintain nice sequential references to your capture groups and if your PCRE engine really is PC, then you can use clustering groups in place of some of the capturing groups:

    (\\d+) ([a-z]+)(?: ([a-z]+)(?: (.+))?)?
    

    Thanks go to Kobi for kindly suggesting this variant. With this version, matching against:

    9112345678 put car details of the car
    

    will yield:

    • "9112345678" in $1
    • "put" in $2
    • "car" in $3
    • "details of the car" in $4

    And that’s probably easier to deal with than accounting for all the extra nesting.

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

Sidebar

Related Questions

I need to try a string against multiple (exclusive - meaning a string that
I tried using this pattern ^[A-z]*[A-z,-, ]*[A-z]* To match against a string that starts
I need to match input strings (URLs) against a large set (anywhere from 1k-250k)
I need to match a string holiding html using a regex to pull out
I need a Perl regular expression to match a string. I'm assuming only double-quoted
I'm trying to match a regex string against a data file in perl, but
I have a regex I need to match against a path like so: C:\Documents
Assuming I've been given the following string to match against: #sidebar h1 ul li{
I would like to know how to match a string against an array of
The code snippet below search allow the user to match a string against three

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.