←back to thread

698 points jgrahamc | 1 comments | | HN request time: 0s | source
Show context
cordite ◴[] No.20422331[source]
That was a fantastic demonstration of what backtracking meant. Thank you John for your in depth description of what went wrong.

As a follow up, would something like `[^=]=.` be a better capture group regex?

replies(1): >>20425233 #
jalada ◴[] No.20425233[source]
Yes. I think HN stole your asterisks. You meant:

  /.*=.*/ becomes /[^=]*=.*/
That is, zero or more 'not-equals-sign-characters', followed by an equals sign.

Where the first regex is 57 steps for x=xxxxxxxxxxxxxxxxxxxxxxxx, the second is just 7.

Avoid using greedy .* for backtracking regex engines! Give your greedy regex engine the hints it needs to do what it does best.

replies(2): >>20426719 #>>20426851 #
1. wbl ◴[] No.20426851[source]
Don't use a backtracking regex engine is probably the better lesson. I want a tool that won't unexpectedly poke me with the sharp bit.