正数php正则表达式,带有0或2位小数的正数的PHP正则表达式(PHP regular expression for positive number with 0 or 2 decimal place...

带有0或2位小数的正数的PHP正则表达式(PHP regular expression for positive number with 0 or 2 decimal places)

我试图使用以下正则表达式来检查字符串是否是一个带零小数位的正数,或2:

^\d+(\.(\d{2}))?$

当我尝试使用preg_match匹配它时,我收到错误:

警告:preg_match():在第862行的/Library/WebServer/Documents/lib/forms.php中找不到结尾分隔符'^'

我究竟做错了什么?

I am trying to use the following regular expression to check whether a string is a positive number with either zero decimal places, or 2:

^\d+(\.(\d{2}))?$

When I try to match this using preg_match, I get the error:

Warning: preg_match(): No ending delimiter '^' found in /Library/WebServer/Documents/lib/forms.php on line 862

What am I doing wrong?

原文:https://stackoverflow.com/questions/3027752

更新时间:2020-01-06 20:52

最满意答案

错误是关于分隔符缺失的是/或#等,确保在分隔符中包装正则表达式:

if (preg_match('/^\d+(\.(\d{2}))?$/', $text))

{

// more code

return true;

}

要么:

if (preg_match('#^\d+(\.(\d{2}))?$#', $text))

{

// more code

return true;

}

The error is about delimiter missing that is / or #, etc, make sure that you wrap regex expression in delimiters:

if (preg_match('/^\d+(\.(\d{2}))?$/', $text))

{

// more code

return true;

}

Or:

if (preg_match('#^\d+(\.(\d{2}))?$#', $text))

{

// more code

return true;

}

2014-09-17

相关问答

放在 。 并将以下数字放入可选组中,并使尾随数字的最小长度为1 \\d+(\\.\\d{1,2})?

Put the . and the following digits into an optional group and make the minimum length of the trailing digits 1 \\d+(\\.\\d{1,2})?

如果要禁用0.00值,并允许不带数字分组符号的数字,则可以使用 /^(?!0+\.0+$)\d{1,3}(?:,\d{3})*\.\d{2}$/.test(your_str)

请参阅正则表达式演示 说明 : ^ - 字符串的开头 (?!0+\.0+$) - 如果输入为零,则表示匹配失败的否定前瞻 \d{1,3} - 1到3位数 (?:,\d{3})* - 0+逗号后跟3位数的序列 \. - 一个字面点 \d{2} - 2位数(小数部分) $ - 结束字符串。 document.body.inn

...

/^(([1-9]|1[0-3])(\.\d\d?)?|14(\.00?)?)$/

https://regex101.com/r/B9eKXN/1 特别是,看看所有似乎都在通过的单元测试。 /^(([1-9]|1[0-3])(\.\d\d?)?|14(\.00?)?)$/

https://regex101.com/r/B9eKXN/1 In particular, look at the unit tests which all seem to be passing.

编辑2:现在完全不允许0,0.0等 这匹配小数点前至少一位数,后跟可选的小数位,后跟0-2位。 负前瞻寻找任何绝对零的味道并防止匹配。 ^(?!0*[.,]0*$|[.,]0*$|0*$)\d+[,.]?\d{0,2}$

这是原始正则表达式,因此您需要为您的语言适当地转义它。 (例如,在某些语言中,您需要将\ sshhes加倍为\\ 。 /^(?!0*[.,]0*$|[.,]0*$|0*$)\d+[,.]?\d{0,2}$/

Edit 2: now disallows exactly 0,0.

...

这应该工作^(\d|(1[0-2]))$ var rxValidHeightInches = new Regex("^(\\d|(1[0-2]))$");

This should work ^(\d|(1[0-2]))$ var rxValidHeightInches = new Regex("^(\\d|(1[0-2]))$");

错误是关于分隔符缺失的是/或#等,确保在分隔符中包装正则表达式: if (preg_match('/^\d+(\.(\d{2}))?$/', $text))

{

// more code

return true;

}

要么: if (preg_match('#^\d+(\.(\d{2}))?$#', $text))

{

// more code

return true;

}

The error is about delimiter missing that is / or #, etc

...

这似乎对我有用: /^[1-9][0-9]?(\.[0-9]{,2})?$/

大卫说,你需要逃避你的. ,或匹配任何字符,包括数字。 This seems to work for me: /^[1-9][0-9]?(\.[0-9]{,2})?$/

As David said, you need to escape your ., or it matches any character, including a digit.

试试以下正则表达式: /^(?:[1-9]|[1-9]?\.(?:25|50|75))$/

它匹配 除零之外的任何整数1-9 1-9或空字符串后跟分数.25,.50,。75,最多为任意数字; 不允许前导零 如果您想匹配更多数字和可选的前导符号,您可以这样做 /^[-+]?(?:[1-9][0-9]*|(?:[1-9][0-9]*)?\.(?:25|50|75))$/

Try the following regex: /^(?:[1-9]|[1-9]?\.(?:25|50|75))$/

It

...

小数部分可以用(?:[.][0-9])? 子模式匹配文字点[.]一个或零个序列,后跟一个数字 ( [0-9]+ )。 但是,您需要对正则表达式进行一些重新分组: 5000必须是一个单独的替代方案,因为它只能有.0小数部分: ^(?:0*(?:[0-9]|[1-9][0-9][0-9]?|[1-4][0-9][0-9][0-9])(?:[.][0-9])?|5000(?:[.]0)?)$

^^ ^^^^

...

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值