[英]Counting and returning Matches with Regular Expression
Consider the following values: 请考虑以下值:
$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
is prefixed with a ','. $format
是否以','为前缀。 This tells me that thousand separators are enabled. I have managed to match the expression (this was not very hard), but what I would like to do is extract the individual matches so I can know whether or not there was a ',' found, and how many zeros there are etc... 我设法匹配了表达式(这不是很困难),但是我想做的是提取单个匹配项,这样我就可以知道是否找到了“,”,以及有多少个零等。 ..
This is what I have so far: 这是我到目前为止的内容:
preg_match_all('/^\,?[0]?[\.]?([0])+?$/',$value['Field_Format'],$matches);
I would use a different regex and put the sub-results into named groups: 我将使用其他正则表达式并将子结果放入命名组中:
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.