Ripgrep: Raise an error on regex with backreference

Created on 5 Dec 2016  路  5Comments  路  Source: BurntSushi/ripgrep

I understand why rg's design fundamentally prevents support for backreferences, but it'd be nice if rg at least printed an error (warning?) message when a regex with a backreference is passed.

question

Most helpful comment

In light of https://github.com/Microsoft/vscode/issues/27802, I think I should re-open this. I've been thinking a lot about this as I rewrite the regex parser, and I regret that the regex engine supports octal escapes at all. It seems like such an incredibly niche feature that it would be much better to just not support them at all in favor of emitting a better failure mode when someone tries to use backreferences.

With that said, ripgrep could certainly decide not to support octal escapes, even if the underlying regex engine does. The current parser doesn't support this kind of introspection, but my rewrite will. Once that's ready for primetime, I'll disable octal escapes and add a better error message inside ripgrep only.

All 5 comments

Doesn't it print an error today?

rg '(foo) \1' prints no error as of 0.3.1 (Arch Linux AUR).

Ah right. Unfortunately, \1 is actually an octal escape sequence, so I don't think that can change at the regex level. We could adopt a heuristic that emits a warning message from within ripgrep, but if someone intended to type an octal escape (which seems quite rare), then the warning would be wrong.

My guess is that more people are going to get burnt from trying a backreference, but I don't feel strongly about it either.

In light of https://github.com/Microsoft/vscode/issues/27802, I think I should re-open this. I've been thinking a lot about this as I rewrite the regex parser, and I regret that the regex engine supports octal escapes at all. It seems like such an incredibly niche feature that it would be much better to just not support them at all in favor of emitting a better failure mode when someone tries to use backreferences.

With that said, ripgrep could certainly decide not to support octal escapes, even if the underlying regex engine does. The current parser doesn't support this kind of introspection, but my rewrite will. Once that's ready for primetime, I'll disable octal escapes and add a better error message inside ripgrep only.

Was this page helpful?
0 / 5 - 0 ratings