Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex Zero or More Spaces Within a Group of Digits

Tags:

regex

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.

like image 654
HoopsMcCann Avatar asked Nov 28 '16 02:11

HoopsMcCann


1 Answers

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

like image 91
dawg Avatar answered Nov 10 '22 13:11

dawg