2
votes

Something maybe wrong with my regular expression (maybe it's looping too much) I think it causes the MVC (C#) web app to timeout as well...

The regEx is:

public const string UrlPartPost = @"^([0-9a-zA-Z-/]*){1,256}$";

I use it like this:

Regex.Match(urlPart3, RegExKeys.UrlPartPost, RegexOptions.IgnoreCase).Success

I added a live test - which is also timing out: https://regex101.com/r/vZ0lN5/1

this is fine: test1-test2-test3-test4-test5

this times out: test1-test2-test3-test4-test_5

How can I fix it so it does not time out?

UPDATE: What is the "*" supposed to do exactly?

1
just [\da-zA-Z-/]+ or [0-9a-zA-Z-/]{1,256}Tim007
I forgot why I had the asterisk in there... not sure what it does, I do need to make sure the string is between 1 and 256 in length...Yovav
Well, I don't see your test string is timing out but not succeeding based on pattern. see regex101.com/r/vZ0lN5/2Saleem

1 Answers

5
votes

The * means match 0+ times, as many as possible.

Your regex is catastrophically backtracking.

Your regex doesn't really make sense, you might want to describe what you're trying to match. You also don't need A-Z and a-z if you are using the case insensitive modifier.

If you want to ensure you have a string with 1-256 of the characters specified, try this: ^([-0-9a-z/]){1,256}$