-
-
Notifications
You must be signed in to change notification settings - Fork 3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Bug with using - inside brackets [] with using Style/RedundantRegexpEscape #8623
Comments
Hi @ebeursken - the issue of However, I wonder if there is a mistake in your regexp - you have
Edit: apologies, this example is incorrect - I had typoed on I don't know what you're trying to match, but maybe |
Thank you for your quick response! @owst |
Hi @ebeursken apologies, I realised I typoed on my example above. It seems Ruby allows redundant nesting of character classes, which I hadn't realised (maybe I can alter
Therefore I believe your regexp:
is equivalent to
and then
|
When creating a regex expression, Rubocop automatically says escaping a dash is not necessary.
When you do not escape a dash (
-
) inside brackets ([]
) it means something else and causes unexpected behavior, since it is seen as a range value instead of an actual dash inside the regex.Thus, this rubocop rule results in regex expressions that are not the same as with escaping those characters!
Expected behavior
No backslash is needed for a
-
And thus within a regex expression, this
-
means an actual -Actual behavior
Not escaping this
-
results in unexpected behavior when using this inside a[]
, so that the regex expression allows something that you did not expect.When using dash inside those type of brackets without backslash, it can mean a range of characters (e.g. [a-d] means a, b, c or d)
See : https://ruby-doc.org/core-2.5.3/Regexp.html#class-Regexp-label-Character+Classes
Steps to reproduce the problem
Code without backslash, according to the rubocop style:
This seems to be a range instead of an actual dash, since it is used inside a []
Code with a backslash
RuboCop version
0.88.0 (using Parser 2.7.1.4, rubocop-ast 0.2.0, running on ruby 2.7.1 x86_64-linux-musl)
The text was updated successfully, but these errors were encountered: