Skip to content
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

Useless non capturing group in regex #164

Open
Haltarys opened this issue Aug 21, 2021 · 2 comments
Open

Useless non capturing group in regex #164

Haltarys opened this issue Aug 21, 2021 · 2 comments

Comments

@Haltarys
Copy link

 var match = /^(-?(?:\d+)?\.?\d+)

Could simply be replaced with:

  var match = /^(-?\d*\.?\d+)

The \d+ matches a digit one or more times. This match is then matched 0 or 1 time with the second ? in (?:\d+)?. This whole sub-regex could simply be expressed as \d* to match a digit 0 or more times.

@Uzlopak
Copy link

Uzlopak commented Mar 6, 2022

I doubt that it is equivalent to the original regex. non-capturing group means, that the group has to exist but is not in the resulting match. So the original regex expects atleast a number value before the dot. Your simplification says, that it can be also no digit at all before the dot.

@Haltarys
Copy link
Author

Haltarys commented Mar 6, 2022

(?:\d+)? == \d* is what I'm saying.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants