![](/img/trans.png)
[英]preg_match(): Compilation failed: nothing to repeat at offset 1 in laravel
[英]preg_match(): Compilation failed: nothing to repeat at offset 2
我不知道為什么我的正則表達式壞了。
我希望它匹配:
$ 100,000
100,000
100000(我知道它與此不匹配,但我希望它匹配)
preg_match("/^\$?(?!0.00)\d{1,3}(,\d{3})*(\.\d\d)?$/", $string);
那么,如何使它也與最后一個匹配,為什么我會收到此錯誤? 這個正則表達式在我的javascript檢查中工作正常。
謝謝
您需要轉義反斜杠以使其原義,因為它在PHP字符串語法中轉義了美元符號。
preg_match("/^\\\$?(?!0.00)\d{1,3}(,\d{3})*(\.\d\d)?$/", $string);
或使用單引號而不是雙引號:
preg_match('/^\$?(?!0.00)\d{1,3}(,\d{3})*(\.\d\d)?$/', $string);
要允許數字不帶逗號,可以使用交替:
preg_match('/^\$?(?!0.00)(\d{1,3}(,\d{3})*|\d+)(\.\d\d)?$/', $string);
加兩個斜杠
preg_match("/^\\\$?(?!0.00)\d{1,3}(,\d{3})*(\.\d\d)?$/", $string,$matches);
^^
<?php
$string='$100,000';
preg_match("/^\\\$?(?!0.00)\d{1,3}(,\d{3})*(\.\d\d)?$/", $string,$matches);
print_r($matches);
OUTPUT :
Array
(
[0] => $100,000
[1] => ,000
)
更新:如果你想匹配,如數字1
, 1000
,等等然后使用下面一個,並注意變化:
preg_match("/^\\\$?(?!0.00)\d{1,3}(\,?\d{3})*(\.\d\d)?$/", $string,$matches);
^ here making the comma optional
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.