How to match 10 numbers with or without spaces with Regex?

Viewed 241

I am looking for a regex expression that matches 10 digits. However, this digit can contain numerous spaces on different places.

A couple of examples that it needs to match are:

7 367151 029

7367151079

60 124 44 11 0

1223 123 5678

As you can see, the spaces are completely random, and there is no way to say how many spaces there will be present. There can also be a lot spaces before the actual digits.

What I have so far is: .*([\d ]{13})

However, this also counts spaces, which results in the regex expression missing the first couple of digits.

Furthermore, it is important that it matches the first 10 numbers, since there is a possibility that a string looks like this:

ESADF 310912 1 23 9 EKJL 129301234812.

2 Answers

A slight variation on @Tranbi's solution, this works even if there is more than one space separating the digits.

(\d\s*?){10}

This pattern should work:

^(\s?\d\s?){10}

If you want to strip leading and trailing whitespaces, and find the sequence anywhere in the string (and not only at the beginning):

\d(\s?\d\s?){8}\d
Related