bennypowers / nvim-regexplainer

Describe the regexp under the cursor
627 stars 7 forks source link

Incorrect interpretation of a negated character list `[^p]` #2

Closed adamazing closed 2 years ago

adamazing commented 2 years ago

Awesome idea! I tried a simple regex in a scratch buffer:

let reggie = /^p[^p]*p/

Regexplainer incorrectly explains it like so:

START
`p`
One of ^, or p (>= 0x)
`p`

image

The first caret is correctly explained as the "start" anchor, but the second caret is interpreted as part of the character list. The first caret inside square brackets actually negates the character list.

So maybe the output of regexplainer would be:

START
`p`
Any character not in the list: `p` (>= 0x)
`p`

And for a character list with multiple characters, e.g. /^p[^p^a]*p/:

START
`p`
Any character not in the list: `p`, `^`, `a` (>= 0x)
`p`

This is how regex101 treats that last example: https://regex101.com/r/mGQXlb/1

bennypowers commented 2 years ago

Thanks for the report. I'll take a look

bennypowers commented 2 years ago

Give that a try and let me know. Thanks again for the interest and the report. 🙏

adamazing commented 2 years ago

Perfect! 🙇 Awesome turn-around time! Treesitter really is neat isn't it?

bennypowers commented 2 years ago

Yup