簡體   English   中英

尺寸的preg_match

[英]preg_match for dimension

我試圖使用pre_match php函數在3維測量中獲得價值,例如7.30 x 7.00 x 4.87 mm

這是ree @([0-9\\.]+) - ([0-9\\.]+) mm x ([0-9\\.]+) mm@

任何人都可以幫助我找到正則表達式的問題。

試試這個代碼:

preg_match("#[\d]+(\.[\d]+)?\s*x\s*[\d]+(\.[\d]+)?\s*x\s*[\d]+(\.[\d]+)?\s*mm#",$variable);

維值是一個浮點數,即由一個或多個數字后跟[\\ d] +可選的一個點和一個或多個數字(。[\\ d] +)組成? 例如:1.45,2,...

整個任選接着進行一個或多個空格\\ S *,隨后任選地隨后一個或多個空格\\ S *x字符。

最后,我們添加mm鏈。

我認為-應該是x並且您嘗試匹配的第一個mm不存在,因此您可以將其刪除:

([0-9\\.]+) x ([0-9\\.]+) x ([0-9\\.]+) mm

請注意, [0-9\\.]+也可以匹配....

如果毫米僅在末端,您也可以這樣嘗試,並重復7.30 x圖案2次,並在末端匹配4.87 mm

您可以在左側和右側使用單詞邊界\\b

\\b(?:[0-9]+\\.[0-9]+ x ){2}[0-9]+\\.[0-9]+ mm\\b

$re = '@\b(?:[0-9]+\.[0-9]+ x ){2}[0-9]+\.[0-9]+ mm\b@';
$str = 'This is a test 7.30 x 7.00 x 4.87 mm test';
if (preg_match($re, $str, $matches)) {
    echo "Match!";
}

通過使用preg_match_all(),您可以做到更准確,更優雅

preg_match_all('/\d+(?:\.\d+)?/', $input, $matches);

if(count($matches[0]) == 3){
 echo 'match!';
}

$ matches [0]將輸出數組中的所有數字

Array
(
    [0] => Array
        (
            [0] => 2.32
            [1] => 1
            [2] => 3.455
        )

)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM