0

The regexp is

^[^\d]*?(\d+)[^\d]*?(\d+)?[^\d]*?$

Am I understanding this right? The ^ and $ at the beginning and end of this expression set the start and endpoints for the expression (meaning that it should be an exact match?). Then inside it's expecting 2 series of non-digits that is followed by any number of digits, and then ends with any non-numeric character?

For example: "My mom has 6 cats and 1 dog."

Also, could somebody please explain the purpose of using capture groups () vs the list [] in this expression?

I apologize if I'm answering my own question, I just want to make sure I'm understanding this.

Barmar
  • 596,455
  • 48
  • 393
  • 495
  • 1
    Your regex and sample data on [regex101](https://regex101.com/r/RoMdCX/1) - full explanation of the regex is provided there – Bravo Nov 07 '19 at 01:47
  • 1
    Start by simplifying `[^\d]` to `\D`, just to make it easier to read – Bergi Nov 07 '19 at 01:47
  • The capturing groups are not necessary for the functioning of the regex itself, but maybe they are used in the evaluation of the match result. Depends on the code that is using this regex. – Bergi Nov 07 '19 at 01:49
  • Your regex will extract `6` and `1` in the first and second capture groups. – Tim Biegeleisen Nov 07 '19 at 01:51
  • Capture groups `()` and character sets `[]` are completely different. A character set just matches a single character that's one of the characters in the list (or not in the list if it begins with `^`). A capture group is for capturing part of a match, or grouping part of a match so you can apply a quantifier to it. – Barmar Nov 07 '19 at 01:51
  • A regexp tutorial like www.regular-expressions.info should explain all these details. – Barmar Nov 07 '19 at 01:51
  • 1
    Your understanding of the regexp is correct. – Barmar Nov 07 '19 at 01:52
  • @Bravo Thanks, I actually started there, but wanted a bit of clarity on the difference between a capture group vs a character set as provided Barmar – AlmightyWill Nov 07 '19 at 04:15
  • @Barmar, Thank you for taking the time to clear that up for me. I'll check the link you provided – AlmightyWill Nov 07 '19 at 04:16

1 Answers1

0

The only thing this Regex definitely cares about is that at the beginning and/or end of the string, there is one group of digits.

^[^\d]*?(\d+)[^\d]*?(\d+)?[^\d]*?$

In front of this group of digits, there could be a series of non-digits.

^[^\d]*?(\d+)[^\d]*?(\d+)?[^\d]*?$

After the initial group of digits, there could also be a series of non-digits...

^[^\d]*?(\d+)[^\d]*?(\d+)?[^\d]*?$

and/or a group of digits...

^[^\d]*?(\d+)[^\d]*?(\d+)?[^\d]*?$

and/or a group of non-digits.

^[^\d]*?(\d+)[^\d]*?(\d+)?[^\d]*?$

If the group of non-digit characters is in the front, that's included. If not, just the initial group of digits. If those following optional parts are there, they're included. If not, the initial group of digits is the end.

Ambrown
  • 168
  • 8