即使有逗號和小數,我也想匹配數字/小數。例如,我希望它能夠在一個句子中匹配。數字可以在句子中間、開頭或結尾:
the number is 1000 today
the number is 1000.00 today
the number is 1000.142 today
the number is 1000.30 today
the number is 1000.200 today
the number is 1,000 today
the number is 1,000.00 today
the number is 1,000.142 today
the number is 1,000.30 today
the number is 1,000.200 today
我試過了,^\d*\.?\d $但似乎不起作用
uj5u.com熱心網友回復:
如果這確實是您的源資料,那么保持簡單怎么樣。
[0-9,.]

uj5u.com熱心網友回復:
用
(?:\d{4,}|\d{1,3}(?:,\d{3})*)(?:\.\d )?
請參閱正則運算式證明。
說明
--------------------------------------------------------------------------------
(?: group, but do not capture:
--------------------------------------------------------------------------------
\d{4,} digits (0-9) (at least 4 times (matching
the most amount possible))
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
\d{1,3} digits (0-9) (between 1 and 3 times
(matching the most amount possible))
--------------------------------------------------------------------------------
(?: group, but do not capture (0 or more
times (matching the most amount
possible)):
--------------------------------------------------------------------------------
, ','
--------------------------------------------------------------------------------
\d{3} digits (0-9) (3 times)
--------------------------------------------------------------------------------
)* end of grouping
--------------------------------------------------------------------------------
) end of grouping
--------------------------------------------------------------------------------
(?: group, but do not capture (optional
(matching the most amount possible)):
--------------------------------------------------------------------------------
\. '.'
--------------------------------------------------------------------------------
\d digits (0-9) (1 or more times (matching
the most amount possible))
--------------------------------------------------------------------------------
)? end of grouping
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/340853.html
標籤:正则表达式
