preg_match:尝试识别数字格式
我不仅试图识别字符串中的“数字”,还尝试标记它是什么类型的数字,例如一般、分数、百分位数、比率等。
现在,如果我使用像 http://rubular.com/,我的模式似乎工作正常。
规则?
([-+]?)([0-9]+)([,+]?)([.]?) //General
([-+]?[0-9.,]+[%]) //Percent
([0-9]+[\/][0-9]+(?:st|nd|rd|th)) //Fraction
([-+]?[0-9.,]+[:][-+]?[0-9.,]+) //Ratio
要检查的字符串?
1
1,000
1.000
-50.5
-1:+3
1,200.6:3.9
+2:-4
1/5th
25%
25.76%
1,001%
当我将它们放入 php (if
/elseif
) 构造中时,我总是得到“一般”数字?
if (preg_match('/([-+]?[0-9.,]+[%])/',$string)) {
echo " PERCENTILE ";
} elseif (preg_match('/([0-9]+[\/][0-9]+(?:st|nd|rd|th))/',$string)) {
echo " FRACTION ";
} elseif (preg_match('/([-+]?[0-9.,]+[:][-+]?[0-9.,]+)/',$string)) {
echo " RATIO ";
} elseif (preg_match('/([-+]?)([0-9]+)([,+]?)([.]?)/',$string)) {
echo " CARDINAL ";
} else {
echo " GENERAL ";
}
不管怎样,它都与“CARDINAL”相呼应。
是的,我尝试过切换顺序(首先是卡/常规,然后是其他)。 是的,我已经尝试使它们成为独立的 IF(),并且以相反的顺序(最一般的首先)。
似乎没有任何作用。 即使对任何格式进行单个 IF 检查也会失败。 要么是我的规则被塞满了——要么是我做错了什么。
I'm trying to identify not only "numbers" in a string, but tag what type of number it is, such as General, Fraction, Percentile, Ratio etc.
Now if I use a tool like http://rubular.com/, my patterns appear to work fine.
Rules?
([-+]?)([0-9]+)([,+]?)([.]?) //General
([-+]?[0-9.,]+[%]) //Percent
([0-9]+[\/][0-9]+(?:st|nd|rd|th)) //Fraction
([-+]?[0-9.,]+[:][-+]?[0-9.,]+) //Ratio
Strings to check?
1
1,000
1.000
-50.5
-1:+3
1,200.6:3.9
+2:-4
1/5th
25%
25.76%
1,001%
It's when I put them in php (if
/elseif
) constructs I always end up with the "general" number?
if (preg_match('/([-+]?[0-9.,]+[%])/',$string)) {
echo " PERCENTILE ";
} elseif (preg_match('/([0-9]+[\/][0-9]+(?:st|nd|rd|th))/',$string)) {
echo " FRACTION ";
} elseif (preg_match('/([-+]?[0-9.,]+[:][-+]?[0-9.,]+)/',$string)) {
echo " RATIO ";
} elseif (preg_match('/([-+]?)([0-9]+)([,+]?)([.]?)/',$string)) {
echo " CARDINAL ";
} else {
echo " GENERAL ";
}
No matter what, it echos "CARDINAL".
Yes, I've tried switching the order (Card/General first, then the others).
Yes, I've tried making them independant IF()s, and in reverse order (most general first).
Nothing appears to work.
Even a Single IF checking for any of the formats simply fails.
Either my rules are stuffed - or I'm doing something blatantly wrong.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的错误一定来自其他地方,我无法重现它。尝试将您的代码放入一个空文件中以自行测试它,它应该可以正常工作。
测试.php:
$ php 测试.php
You error must come from somewhere else, I couldn't reproduce it. Try to put your code in an otherwise empty file to test it by itself and it should work fine.
Test.php:
$ php test.php
尝试用
^
和$
包围每个正则表达式,以便仅当它匹配整个字符串或行时才匹配。如果没有这些,如果正则表达式匹配数字的一部分,那么它仍然是匹配的。例如,'1,000'
与基数数字的正则表达式/([-+]?)([0-9]+)([,+]?)([. ]?)/
,因为它匹配子字符串'1,'
。如果添加^
和$
,如/^([-+]?)([0-9]+)([,+]?) ([.]?)$/
,那么它不再匹配。输出:
编辑:这可能是基数数字的更好正则表达式:
Try surrounding each regular expression with
^
and$
so that it matches only if it matches the entire string or line. Without these, if a regular expression matches part of a number, then that's still a match. For example,'1,000'
matches your regular expression for CARDINAL numbers,/([-+]?)([0-9]+)([,+]?)([.]?)/
, because it matches the substring'1,'
. If you add^
and$
, as in/^([-+]?)([0-9]+)([,+]?)([.]?)$/
, then it no longer matches.outputs:
EDIT: This might be a better regular expression for CARDINAL numbers: