Regex to match digits and at most one space between them

regex single space
regex find space between words
regex whitespace
regex match
regex any number of digits
regex space between numbers
regex digit
python regex

I am trying to match phone numbers in the following patterns:

9 99 99 99 99
0999999999
11 0999999999
9 9999 9999

But not the following:

9 99  99 99 99 (two spaces)
9 99\n99 99 99 

Therefore, I want to match 7 to 12 digits and an optional spaces between them, but not sequences of more than one white space.

So far I came up with "[\d ?]{7,12}", but it doesn't really match the requirements as the spaces are counted in the {7,12} and it also matches two sequences of spaces.

python, [\d ?]{7,12} is a pattern that matches 7 to 12 digit, space or ? chars. It can match a ??????? string because ? is not a quantifier, but a mere  With some variations depending on the engine, regex usually defines a word character as a letter, digit or underscore. A word boundary \b detects a position where one side is such a character, and the other is not. In the everyday world, most people would probably say that in the English language, a word character is a letter. Others might

I'd try

(?:\d+ ?){7,12}

The original regex was matching a character group of a space OR a digit seven to twelve times. The supplied regex matches a digit followed by a possible space seven to twelve times. That way the spaces aren't counted as part of the total.

RegexOne - Lesson 9: All this whitespace, RegexOne The most common forms of whitespace you will use with regular expressions are the space (␣) environments), and these special characters match each of their respective whitespaces. Try writing a pattern that can match each line containing whitespace characters between the number and the content. Match One or More Times: + The + quantifier matches the preceding element one or more times. It is equivalent to {1,}.+ is a greedy quantifier whose lazy equivalent is +?. For example, the regular expression \ban+\w*?\b tries to match entire words that begin with the letter a followed by one or more instances of the letter n.

you can use

\d(\s?\d){6,11}

the first \d matches on the first digit. Next can follow a group of 6 to 11 (to make a total of 7 to 12) pairs of an optional space, followed by a digit. Multiple spaces are not allowed, as you see each optional space has digits to both sides. It can be checked here That regexp is equivalent, but shorter, to this one:

\d\s?\d\s?\d\s?\d\s?\d\s?\d((((((\s?\d)?\s?\d)?\s?\d)?\s?\d)?\s?\d)?\s?\d)?

that can be checked here.

NOTE

See that the \s matches a newline, so you can get multiline number (as shown in the examples) If you don't like that behaviour, then narrow the space class using a simple space, as in

\d( ?\d){6,11}

that can be tested here Look, that now, a more than 12 digits number is truncated to only the first twelve, if this is not desired, use word boundary at the end, as in

\d( ?\d){6,11}\b

See it here.

Regexp Tutorial, In most flavors that support Unicode, \d includes all digits from all scripts. Notable exceptions are These Unicode flavors match only ASCII digits with \d. \w stands for “word [\s\d] matches a single character that is either whitespace or a digit. Solution We have to match only the lines that have a space between the list number and 'abc'. We can do that by using the expression \d\.\s+abc to match the number, the actual period (which must be escaped), one or more whitespace characters then the text.

Everything you need to know about Regular Expressions, Before you use regular expressions in your code, you can test them using an Since the class contains alternatives, it matches exactly one character. It matches every position between characters within white space and  Let’s say we have a string like +7 (903)-123-45-67 and want to find all numbers in it. But unlike before, we are interested not in single digits, but full numbers: 7, 903, 123, 45, 67. A number is a sequence of 1 or more digits \d. To mark how many we need, we can append a quantifier. The simplest quantifier is a number in curly braces: {n}.

Regular expressions in Perl, +, match 1 or more times ? match 0 In the table above, the characters themselves, in the first column, are links to \S, matches any non-whitespace character. So a pattern of \d\d\d-\d\d-\d\d\d\d will match a USA social security number. Three digits, then a dash, two digits, then a dash and then 4 digits. There are better and more compact ways to represent that same pattern. But this will work for our examples today.

REGEXREPLACE( ) function, Replaces all instances of strings matching a regular expression with a pattern can contain literal characters, metacharacters, or a combination of the two. one or more spaces between text characters with a single space. In most regex flavors, a lookbehind must have a fixed number of characters, or at least a number of characters within a specified range. However, a handful of flavors allow true variable-width lookbehinds. Among the chosen few are .NET, Matthew Barnett's alternate regex module for Python and JGSoft (available in RegexBuddy and EditPad).

Comments
  • Try \b\d(?: ?\d){6,9}\b
  • I think that's it, thanks! I edited the question because I actually meant 12, not 10 but the idea is the same
  • So you want to match spaces (the space character) but not arbitrary whitespace (space, tab, newline, etc.)? You may want to remove the word white from your title and question in that case. It's misleading.
  • Done. I guess I didn't know the real meaning of "white space"