preg_match:尝试识别数字格式

发布于 2024-12-07 14:31:17 字数 1167 浏览 0 评论 0原文

我不仅试图识别字符串中的“数字”,还尝试标记它是什么类型的数字,例如一般、分数、百分位数、比率等。

现在,如果我使用像 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

無心 2024-12-14 14:31:17

您的错误一定来自其他地方,我无法重现它。尝试将您的代码放入一个空文件中以自行测试它,它应该可以正常工作。

测试.php:

$nums = array(
  '1',
  '1,000',
  '1.000',
  '-50.5',
  '-1:+3',
  '1,200.6:3.9',
  '+2:-4',
  '1/5th',
  '25%',
  '25.76%',
  '1,001%'
);
foreach ($nums as $string) {
  echo $string.': ';
  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 ";
  }
  echo "\n";
}

$ php 测试.php

1:  CARDINAL 
1,000:  CARDINAL
1.000:  CARDINAL
-50.5:  CARDINAL
-1:+3:  RATIO 
1,200.6:3.9:  RATIO
+2:-4:  RATIO 
1/5th:  FRACTION 
25%:  PERCENTILE
25.76%:  PERCENTILE 
1,001%:  PERCENTILE

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:

$nums = array(
  '1',
  '1,000',
  '1.000',
  '-50.5',
  '-1:+3',
  '1,200.6:3.9',
  '+2:-4',
  '1/5th',
  '25%',
  '25.76%',
  '1,001%'
);
foreach ($nums as $string) {
  echo $string.': ';
  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 ";
  }
  echo "\n";
}

$ php test.php

1:  CARDINAL 
1,000:  CARDINAL
1.000:  CARDINAL
-50.5:  CARDINAL
-1:+3:  RATIO 
1,200.6:3.9:  RATIO
+2:-4:  RATIO 
1/5th:  FRACTION 
25%:  PERCENTILE
25.76%:  PERCENTILE 
1,001%:  PERCENTILE
烟雨扶苏 2024-12-14 14:31:17

尝试用 ^$ 包围每个正则表达式,以便仅当它匹配整个字符串或行时才匹配。如果没有这些,如果正则表达式匹配数字的一部分,那么它仍然是匹配的。例如,'1,000' 与基数数字的正则表达式 /([-+]?)([0-9]+)([,+]?)([. ]?)/,因为它匹配子字符串 '1,'。如果添加 ^$,如 /^([-+]?)([0-9]+)([,+]?) ([.]?)$/,那么它不再匹配。

<?php
function check_format($string) {
    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 ";
    }
}

array_map("check_format", array(
    "1",
    "1,000",
    "1.000",
    "-50.5",
    "-1:+3",
    "1,200.6:3.9",
    "+2:-4",
    "1/5th",
    "25%",
    "25.76%",
    "1,001%"
    ));

输出:

 CARDINAL  GENERAL  GENERAL  GENERAL  RATIO  RATIO  RATIO  FRACTION  PERCENTILE  PERCENTILE  PERCENTILE 

编辑:这可能是基数数字的更好正则表达式:

/^([-+]?)([0-9]+)(?:,?[0-9]{3})*([.]?)$/

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.

<?php
function check_format($string) {
    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 ";
    }
}

array_map("check_format", array(
    "1",
    "1,000",
    "1.000",
    "-50.5",
    "-1:+3",
    "1,200.6:3.9",
    "+2:-4",
    "1/5th",
    "25%",
    "25.76%",
    "1,001%"
    ));

outputs:

 CARDINAL  GENERAL  GENERAL  GENERAL  RATIO  RATIO  RATIO  FRACTION  PERCENTILE  PERCENTILE  PERCENTILE 

EDIT: This might be a better regular expression for CARDINAL numbers:

/^([-+]?)([0-9]+)(?:,?[0-9]{3})*([.]?)$/
玩物 2024-12-14 14:31:17
Please have a look on this .

<?php 
$num = 12.20

$num = floatval(preg_replace('/[^\d.]/', '', num));

// output will be 12.2

//it always replace zero and you have number 12.00 then output will be 12
$num = 12.00

$num = floatval(preg_replace('/[^\d.]/', '', num));
// output will be 12
Please have a look on this .

<?php 
$num = 12.20

$num = floatval(preg_replace('/[^\d.]/', '', num));

// output will be 12.2

//it always replace zero and you have number 12.00 then output will be 12
$num = 12.00

$num = floatval(preg_replace('/[^\d.]/', '', num));
// output will be 12
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文