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

130
Visualizações
Why does checking this string with Regex.IsMatch cause CPU to reach 100%?

When using Regex.IsMatch (C#, .Net 4.5) on a specific string, the CPU reaches 100%.

String:

https://www.facebook.com/CashKingPirates/photos/a.197028616990372.62904.196982426994991/1186500984709792/?type=1&permPage=1

Pattern:

^http(s)?://([\w-]+.)+[\w-]+(/[\w- ./?%&=])?$

Full code:

Regex.IsMatch("https://www.facebook.com/CashKingPirates/photos/a.197028616990372.62904.196982426994991/1186500984709792/?type=1&permPage=1",
                @"^http(s)?://([\w-]+.)+[\w-]+(/[\w- ./?%&=])?$");

I found that redacting the URL prevents this problem. Redacted URL:

https://www.facebook.com/CashKingPirates/photos/a.197028616990372.62904.196982426994991/1186500984709792

But still very interested in understanding what causes this.

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

As nu11p01n73R pointed out, you have a lot backtracking with your regular expression. That’s because parts of your expression can all match the same thing, which gives the engine many choices it has to try before finding a result.

You can avoid this by changing the regular expression to make individual sections more specific. In your case, the cause is that you wanted to match a real dot but used the match-all character . instead. You should escape that to \..

This should already reduce the backtracking need a lot and make it fast:

^http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&=])?$

And if you want to actually match the original string, you need to add a quantifier to the character class at the end:

^http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]+)?$
                                           ↑
over 4 years ago · Santiago Trujillo Relatório

0

I suggest you to check http://regexr.com/ website, to test your regular expression.

The corrected version of your regular expression is this:

^(https?://(?:[\w]+\.?[\w]+)+[\w]/?)([\w\./]+)(\?[\w-=&%]+)?$

It also has 3 groups:

  1. group1=Main url (for example: facebook.com)
  2. group2=Sub urls (for example: /CashKingPirates/photos/a.197028616990372.62904.196982426994991/1186500984709792/
  3. group3=Variables (for example: ?type=1&permPage=1)

Also remember for checking actual character of dot (.) in your regular expression you must use \. not .

over 4 years ago · Santiago Trujillo Relatório

0

Your regex suffers for catastrophic backtracking.You can simply use

^http(s)?://([\w.-])+(/[\w ./?%&=-]+)*$

See demo.

https://regex101.com/r/cK4iV0/15

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