R.hagens
R.hagens

Reputation: 375

Regex match certain amount of character and allow space inbetween

I am currently working on a regex which needs to match exactly 8 digits. But sometimes it occurs that there are spaces or dots between those numbers. This is the regex that i am currently using.

([0-9\ ?.?]{7,16})

It works fine most of the time, but the problem I am having is that it sometimes matches number with a lot of spaces tailing it so you will get something like 1234/s/s/s/s (/s stands for space) Or sometimes it is only matching spaces.

What i want is a regex that always matches at least 8 digits and also allows spaces and dots without detecting less then 8 digits. I know it may be stupid question, but I couldn't find anything I need elswhere.

Upvotes: 2

Views: 653

Answers (2)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626774

Your ([0-9\ ?.?]{7,16}) expression matches 7 to 16 occurrences of any character that is either a digit, or a space, or a ?, or .. Yes, the ? inside [...] is a literal ?, not a quantifier.

You need to use an expression that will match a digit ([0-9]) and then exactly 7 sequences of a space or period ([ .]) followed with 1 digit, and to make sure you are not matching the digits in 123.156.78.146 you may use special boundaries:

(?<!\d[ .]?)\d(?:[. ]?\d){7}(?![ .]?\d)

if the space or . can only be 0 to 1 in between digits; or - if the space/dot can appear 0 or more times,

(?<!\d[ .]*)\d(?:[. ]*\d){7}(?![ .]*\d)

See the regex demo

The (?<!\d[ .]*) is a negative lookbehind that will fail any match if it starts with a digit that is followed with .(s) or space(s), and the (?![ .]*\d) negative lookahead will fail the match if the 7 digits you need are followed with .(s) or space(s) and a digit.

Upvotes: 3

AdrianHHH
AdrianHHH

Reputation: 14038

To solve this, describe the problem to yourself. You want to match one digit followed by seven repetitions of space-or-dot followed by a digit. This leads to a regular expression like \d([ .]?\d){7}. To avoid collecting the seven captures add a :? after the (. To capture the whole string, enclose it in brackets. Adding both changes gives the expression (\d(:?[ .]?\d){7}). If more than one space or dot is allowed between the digits then change the ? to a *.

To get just the eight digits out of the string I suggest using the string captured above and replacing any spaces or dots with nothing.

Upvotes: 1

Related Questions