tags:

views:

36

answers:

2

Hi guys,

I would like to have a regular expression which returns only the numbers and the white space which I've inserted. i.e.

012 345 678 

or

012 345 678 910.

Any suggestion please?

+1  A: 

You could remove any other character except digits and white space. Here’s an example in PHP:

$output = preg_replace('/[^\d\s]/', '', $str);

Then the remaining string does only consist of digits and white space characters.

Gumbo
A: 

A Java example which uses the regular expression (\d+(\s\d)?)*:

Pattern regex = Pattern.compile("(\\d+(\\s\\d)?)*", Pattern.CANON_EQ);
Matcher regexMatcher = regex.matcher("i.e 012 345 678 or 012 345 678 910.");
if (regexMatcher.find()) {
    resultString = regexMatcher.group();
} 
splash