suppose I have a sequence of one to three digits that can have any number of spaces in between them, and suppose that these numbers are within a group that I can back reference. How would I go by doing this? Here's what I have so far
([\d\s*]{1,3})
I'm just a bit confused as to how I'm to have a pattern that matches up to three digits, have zero or more spaces between then, and keep them within a group.
Anyway, thanks.
You can do:
((?:\d\s*){1,3})
Demo
Explanation:
((?:\d\s*)){1,3}
^ ^ define a non capturing group
^ a single digit
^ a space zero or more times
^ ^ capture that group (digit and following space pattern)
^ 1 to 3 times
You can also do:
^(\d\s*\d?\s*\d?\s*)
^ ^ capture group
^ one digit
^ zero or more spaces
^ optional digit
^ zero or more spaces
^ ^ etcetera.....
Demo
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With