Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

399
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda