regexp-tree icon indicating copy to clipboard operation
regexp-tree copied to clipboard

regexp-tree does not optimize `(\.(?!$)|$))$`

Open kurtextrem opened this issue 1 year ago • 2 comments

Coming from this SO post: https://stackoverflow.com/a/36760050 The regex

^((25[0-5]|(2[0-4]|1\d|[1-9]|)\d)(\.(?!$)|$)){4}$

got optimized (by hand) to:

^((25[0-5]|(2[0-4]|1\d|[1-9]|)\d)\.?\b){4}$

so, partbefore(\.(?!$)|$))$ can be expressed as (partbefore\.?\b)$, avoiding the negative look-ahead, making the regex smaller.

Maybe worth adding as optimization?

kurtextrem avatar Oct 20 '23 12:10 kurtextrem

cross-referencing this reply from eslint-plugin-regexp: https://github.com/ota-meshi/eslint-plugin-regexp/issues/659#issuecomment-1773046245

The regex is actually not faster, for different reasons. I'm not sure what regexp-tree optimizes for (fastest runtime or smallest regex), so ymmv if or what action you'd want to take. In any case, I want to avoid duplicate work while investigation :)

kurtextrem avatar Oct 21 '23 08:10 kurtextrem

@kurtextrem thanks for the report. Yes, this would be nice to add - feel free to send a PR in case.

DmitrySoshnikov avatar Oct 22 '23 23:10 DmitrySoshnikov