开发者

Struggling with number regex match

开发者 https://www.devze.com 2022-12-21 17:31 出处:网络
I\'m tryin开发者_如何学JAVAg 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?

I'm tryin开发者_如何学JAVAg 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


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.


........

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


Also you can use:

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

\s stands for space char

0

精彩评论

暂无评论...
验证码 换一张
取 消