簡體   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