繁体   English   中英

用正则表达式计算和返回匹配项

[英]Counting and returning Matches with Regular Expression

请考虑以下值:

$format = ',0';        // Thousand separators ON, no decimal places 
$format = '0';         // Thousand separators OFF, no decimal places
$format = '0.000';     // Thousand separators OFF, 3 decimal places
$format = ',0.0';      // Thousand separators ON, 1 decimal place
  1. 首先要做的是查看$format是否以','为前缀。 这告诉我启用了千个分隔符。
  2. 其次,我必须查看第一个零之后有多少个零。 例如,以下将是2个小数位“ 0.00”等。

我设法匹配了表达式(这不是很困难),但是我想做的是提取单个匹配项,这样我就可以知道是否找到了“,”,以及有多少个零等。 ..

这是我到目前为止的内容:

preg_match_all('/^\,?[0]?[\.]?([0])+?$/',$value['Field_Format'],$matches);

我将使用其他正则表达式并将子结果放入命名组中:

if (preg_match(
    '/^
    (?P<thousands>,)? # Optional thousands separator
    0                 # Mandatory 0
    (?:               # Optional group:
     (?P<decimal>\.)  # Decimal separator
     (?P<digits>0+)   # followed by one or more zeroes
    )?                # (optional)
    $                 # End of string/x', 
    $subject, $regs)) {
    $thousands = $regs['thousands'];
    $decimal = $regs['decimal'];
    $digits = $regs['digits'];
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM