I guess there are already a bunch of length safety check in other places, but if you are going to leave length check in the code it makes sense to move it on top of the regex check.

This way, we won't potentially spend a lot time checking the regex if user passes in some really long string with a pathological use case.