tags:

views:

45

answers:

3

Hi,

I'm trying to match only numbers and spaces in my php regex but the following fails and I can't seem to understand why, can anyone shed some light on what I'm doing wrong please?

$pattern = '/^[0-9\ ]$/';

Thanks

+1  A: 

Your regular expression just describes one single character. You might want to add a quantifier like +:

'/^[0-9\ ]+$/'

This describes a string of one or more digits or space characters.

Gumbo
I don't think you need to escape the space. I may be wrong.
Yacoby
@Yacoby: Yes, you’re right. The space does not need to be escaped.
Gumbo
+1  A: 

........

$pattern = '/^[\d\ ]+$/';
Sarfraz
anything wrong there????, would love to learn, thanks...
Sarfraz
There was before the edit. The downvote has now been removed.
Yacoby
@Yacoby: you are right, thanks
Sarfraz
A: 

Also you can use:

'/^[0-9\s]+$/'

\s stands for space char

Gmi182