检查字符串是否只有数字和/或至少一位数字后有一位小数,句点后最多允许 3 个数字
Check if the string is only numbers and or has one decimal after at least one digit and after the period allow up to 3 numbers
我正在尝试创建一个 reg ex 模式来匹配一个字符串,该字符串在可选小数点前至少有一个数字,最多有两个数字,如果包含小数点,则它们应该至少为一到三后面最多数字。
例如,以下值应该有效。
0
0.0
0.00
0.000
00
00.0
00.00
00.000
以下值不应有效。
0.
00.
0.0000
00.0000
到目前为止,这是我的代码
function check($string){
if(preg_match('/^[0-9][0-9]?(?:\.[0-9]{1,3})$/', $string)){
return true;
} else {
return false;
}
}
您可以使用这个正则表达式
^[0-9]{1,2}(?:\.[0-9]{1,3})?$
组符号的同义词 \d
^\d{1,2}(?:\.\d{1,3})?$
您可以使用更简单的正则表达式。
<?php
$tests = [ '0',
'0.0',
'0.00',
'0.000',
'00',
'00.0',
'00.00',
'00.000',
'0.',
'00.',
'0.0000',
'00.0000'
];
$regex_pattern = '/^\d{1,2}(\.\d{1,3})?$/';
foreach($tests as $each_test){
echo $each_test," -----------> ",var_dump(1 === preg_match($regex_pattern,$each_test)),"<br/>";
}
输出:
0 -----------> bool(true)
0.0 -----------> bool(true)
0.00 -----------> bool(true)
0.000 -----------> bool(true)
00 -----------> bool(true)
00.0 -----------> bool(true)
00.00 -----------> bool(true)
00.000 -----------> bool(true)
0. -----------> bool(false)
00. -----------> bool(false)
0.0000 -----------> bool(false)
00.0000 -----------> bool(false)
'/^\d{1,2}(\.\d{1,2})?$/'
正是您要搜索的模式。
但很多时候最好编写一个函数来保持整洁和简单,以防您想扩展这个想法
function OptionalDecimal($numstr) {
//use string count to check for the times decimal points appears in the string
if (substr_count($numstr,".") == 0) {
if(preg_match('/^\d{1,2}$/', $numstr)) return 'true';
}
if (substr_count($numstr,".") == 1) {
if(preg_match('/^\d{1,2}\.\d{1,2}$/', $numstr)) return 'true';
}
return 'false';
}
echo OptionalDecimal("4.7");
这个函数做同样的事情
我正在尝试创建一个 reg ex 模式来匹配一个字符串,该字符串在可选小数点前至少有一个数字,最多有两个数字,如果包含小数点,则它们应该至少为一到三后面最多数字。
例如,以下值应该有效。
0
0.0
0.00
0.000
00
00.0
00.00
00.000
以下值不应有效。
0.
00.
0.0000
00.0000
到目前为止,这是我的代码
function check($string){
if(preg_match('/^[0-9][0-9]?(?:\.[0-9]{1,3})$/', $string)){
return true;
} else {
return false;
}
}
您可以使用这个正则表达式
^[0-9]{1,2}(?:\.[0-9]{1,3})?$
组符号的同义词 \d
^\d{1,2}(?:\.\d{1,3})?$
您可以使用更简单的正则表达式。
<?php
$tests = [ '0',
'0.0',
'0.00',
'0.000',
'00',
'00.0',
'00.00',
'00.000',
'0.',
'00.',
'0.0000',
'00.0000'
];
$regex_pattern = '/^\d{1,2}(\.\d{1,3})?$/';
foreach($tests as $each_test){
echo $each_test," -----------> ",var_dump(1 === preg_match($regex_pattern,$each_test)),"<br/>";
}
输出:
0 -----------> bool(true)
0.0 -----------> bool(true)
0.00 -----------> bool(true)
0.000 -----------> bool(true)
00 -----------> bool(true)
00.0 -----------> bool(true)
00.00 -----------> bool(true)
00.000 -----------> bool(true)
0. -----------> bool(false)
00. -----------> bool(false)
0.0000 -----------> bool(false)
00.0000 -----------> bool(false)
'/^\d{1,2}(\.\d{1,2})?$/'
正是您要搜索的模式。
但很多时候最好编写一个函数来保持整洁和简单,以防您想扩展这个想法
function OptionalDecimal($numstr) {
//use string count to check for the times decimal points appears in the string
if (substr_count($numstr,".") == 0) {
if(preg_match('/^\d{1,2}$/', $numstr)) return 'true';
}
if (substr_count($numstr,".") == 1) {
if(preg_match('/^\d{1,2}\.\d{1,2}$/', $numstr)) return 'true';
}
return 'false';
}
echo OptionalDecimal("4.7");
这个函数做同样的事情