Rich
Rich

Reputation: 209

How to match one or more characters nominated in a regex character class?

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\ ]$/';

Upvotes: 0

Views: 282

Answers (3)

Gmi182
Gmi182

Reputation: 41

Also you can use:

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

\s stands for space char

Upvotes: 0

Sarfraz
Sarfraz

Reputation: 382909

........

$pattern = '/^[\d\ ]+$/';

Upvotes: 1

Gumbo
Gumbo

Reputation: 655825

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.

Upvotes: 1

Related Questions