[英]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.