Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

398
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda