I'm trying to find a unique number from string which contain 4 numbers seprated by spaces in between them (not at start & end) & occurrence of these numbers should be 3.
I've tried like this but it gives me numbers with & without spaces which I don't want it should contain spaces in between them.
Example
(\d{4}.?){3}
above regex selects these as correct
2131 2312 3675
2131231212313675
2131 1231 3675
- (this includes spaces at start & end)
In option (3) I can ignore spaces but I don't want output as option (2).
How can I fix this?
Live example
CodePudding user response:
Converting my comment to answer so that solution is easy to find for future visitors.
You may use this regex:
\b\d{4}(?: \d{4}){2}\b
RegEx Breakup:
\b
: Word boundary\d{4}
: Match 4 digit(?: \d{4}){2}
: Match a space followed by 4 digits. Repeat this group 2 times to make sure to match 3 sets separated by a single space.\b
: Word boundary
CodePudding user response:
This should work: (?:\s*\b\d{4}\b){4}