[英]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
$format
是否以','为前缀。 这告诉我启用了千个分隔符。 我设法匹配了表达式(这不是很困难),但是我想做的是提取单个匹配项,这样我就可以知道是否找到了“,”,以及有多少个零等。 ..
这是我到目前为止的内容:
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.