6 ms·
Not understanding why backtracking happened. Once it hit a non space, non end character, move on. Nothing before can match the regex.
by unethical_ban 10y ago
Not understanding why backtracking happened. Once it hit a non space, non end character, move on. Nothing before can match the regex.
- sklivvz1971 10y agoIt finds a space, tries to match the rest of the string, fails, rolls back to it and goes forward one, finds a space... The trick is that there's no guarantee, in general, that a match failing at character N is due to character N, so the regex engine backtracks.
- unethical_ban 10y agoI guess I need to buff up on my automata, but I would have thought that for positional delimiters, there would be optimizations. If the pattern is \s+$, then I can look from the back of the line, see if there is a space, and if so, go backward until I find a non-space character.
- viraptor 10y agoThat's just special casing the end of line. If it was "\s+x" you'd still have the same problem.