我想知道为什么此正则表达式不能在kohana上工作以检索浮点负数。
正则表达式:
^(-|)\d+((.|,)\d+|)$
Kohana路线:
Route::set('route','data/<data>',array("data"=>"^(-|)\d+((.|,)\d+|)$"));
我在regex101上尝试了此正则表达式,并且可以正常运行,知道为什么它不在kohana路线上吗?
谢谢
您的正则表达式不必要地复杂,并且有错误。将.
在(.|,)
不匹配的时期,但任何字符(点在正则表达式的含义,字符类之外)。我们要么需要转义点(如中)\.
,要么将其包括在字符类中(如中)。[.,]
请尝试使用以下紧凑型正则表达式:
^-\d+[.,]\d+$
或者^-\d+([.,]\d+)?$
如果您想使小数部分为可选。
您原来的正则表达式建议您要允许使用逗号。如果没有,请使用以下命令:
^-\d+\.\d+$
或者^-\d+(\.\d+)?$
如果您想使小数部分为可选。
解释正则表达式
^ # the beginning of the string
- # '-'
\d+ # digits (0-9) (1 or more times (matching
# the most amount possible))
[.,] # any character of: '.', ','
\d+ # digits (0-9) (1 or more times (matching
# the most amount possible))
$ # before an optional \n, and the end of the
# string
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句