in reply to why this regular expression is so slow?

The reason it's slow is that you're anchoring the regexp to the end, but you have a other matches in the string which only fail because they don't end at the end of the string.

The obvious answer here is: don't use a regexp. It seems you just want the last four characters in the string that aren't dashes or dots. You could simple remove the dashes and dots from the string, and take the last four characters from the resulting string. (You may need some special handling if the string may contain less than four such characters).

  • Comment on Re: why this regular expression is so slow?

Replies are listed 'Best First'.
Re^2: why this regular expression is so slow?
by Corion (Patriarch) on Jul 15, 2009 at 09:44 UTC

    Alternatively, sexeger points out the approach of reversing the input and then taking the first four non-dash/dots characters, which should also happen without much backtracking.