Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

396
Views
PHP - Preg match with multiple conditions

I have the following the following string:

"#(admin/pages|admin/pages/add|admin/pages/[0-9]+)#"

And this string to compare it to:

"admin/pages/1"

What I need is to return "admin/pages/1" when comparing the 2 strings using preg_match(). I have the following code and it's not working:

if(preg_match("#(admin/pages|admin/pages/add|admin/pages/[0-9]+)#", "admin/pages/1", $matches) {
   var_dump($matches); 
}

This is the output i get:

array(2) { [0]=> string(11) "admin/pages" [1]=> string(11) "admin/pages" }

Can anybody help out?

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Change your regex to:

"#(admin/pages(?:/\d+)?|admin/pages/add)#"

You don't need both variants (admin/pages|admin/pages/[0+9]+) if you put the digits in the first pattern and make them optional.

Question marks and repetitions are greedy by default, that's why it will always include the digits in the match for my version.
On the other hand, if you have an alternation, it will always pick the first match. Since your first alternation does not include digits, they are not matched.

If you're also wondering why you get your match two times, that's because of the way preg_match works.
Quote from the documentation:

$matches[0] will contain the text that matched the full pattern, $matches[1] will have the text that matched the first captured parenthesized subpattern, and so on.

You can remove the outer parentheses if the whole match is enough:

"#admin/pages(?:/\d+)?|admin/pages/add#"

Just use $matches[0].

And, as @RomanPerekhrest has written and I shamelessly include in this answer, you can shorten your pattern. You don't need to include admin/pages multiple times:

"#admin/pages(?:/add|/\d+)?#"
over 4 years ago · Santiago Trujillo Report

0

Use the following short regex parrent:

"#admin/pages(/add|/[0-9]+)?#"

(/add|/[0-9]+)? - optional alternative group, matches either /add or /<number> at the end of searched substring if occurs

over 4 years ago · Santiago Trujillo Report

0

Try changing the order of components:

"#(admin/pages/[0-9]+|admin/pages/add|admin/pages)#"

The regular expression is satisfied as soon as something matches. In your case, it stopped as soon as it found admin/pages without looking any further.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!