我需要在AS3中編寫一個RegExp,它將Excel格式的貨幣值解析爲一個數字: E.g. 正則表達式($ 35,600.00)= 35600RegExp解析貨幣值
並檢查它是否格式正確(以「,」作爲千位分隔符,「。」作爲小數點。貨幣符號可以是任意的(不只是$)和可以忍受的開頭或結尾。
所以我只需要從數量剝去每一個非數字,檢查是否是有效的。
謝謝! 馬丁
我需要在AS3中編寫一個RegExp,它將Excel格式的貨幣值解析爲一個數字: E.g. 正則表達式($ 35,600.00)= 35600RegExp解析貨幣值
並檢查它是否格式正確(以「,」作爲千位分隔符,「。」作爲小數點。貨幣符號可以是任意的(不只是$)和可以忍受的開頭或結尾。
所以我只需要從數量剝去每一個非數字,檢查是否是有效的。
謝謝! 馬丁
你需要2箱子,一個用逗號分隔,另一個用十進制分隔整數
如果是整數,請將逗號或小數點後的所有內容(取決於您的格式)刪除。然後運行下面的正則表達式:
這將刪除所有的非數字字符:
s/\D+//g;
如果你沒有一個整數,你將需要包括整個數字分隔異常:
小數分隔:
s/[^\d.]+//g
逗號分隔符:
s/[^\d,]+//g
*免責聲明:我只是在我的腦海中解析這些正則表達式,所以我的語法可能會稍微偏離。
[$|£|<insert more pipe sepatared options here>]?(\d)*(,)?(\d)*.\d\d[$|£|<insert more pipe sepatared options here>]?
可能工作。
剝離前導和尾隨貨幣符號和空格後,可以使用以下表達式。
[1-9][0-9]{1,2}(,[0-9]{3})*(.[0-9]{2})+
也就是說
( [1-9][0-9]{0,2} One to three digits, no leading zero, followed by (,[0-9]{3})* a thousands separator and three digits, zero or more times, | or 0 a leading zero. ) (.[0-9]{2})? Optional a decimal point followed by exactly two digits.
處理貨幣符號好聽點是不是最容易的事情,因爲你必須避免與領先和後貨幣符號輸入。解決方案將使用前瞻斷言。
(?=$(([$€] ?)?[0-9,.]+|[0-9,.]+(?[$€]))^)[$€ ]+<ExpressionFromAbove>[$€ ]+
這樣做如下。
(?= Positive look ahead assertion. $ Anchor start of line. ( Begin options. ([$€] ?)? Optional leading currency symbol followed by an optional space [0-9,.]+ and one or more digits, thousand separators, and decimal points | or [0-9,.]+ one or more digits, thousand separators, and decimal points (?[$€]) followed by an optional space and and a currency symbol. ) End options. ^ Anchor end of line. ) [$€ ]+ Leading currency symbol and optional space. <ExpressionFromAbove> Match the actual number. [$€ ]+ Trailing optional space and currency symbol.
如果你知道,該格式是正確的,去掉一切,是不是數字或小數點,它解析爲一個小數(這將被使用在C#Decimal.Parse()),或,如果沒有合適的解析方法,只需在小數點處拆分,解析爲整數,然後合併兩個數字。
這也將照顧,如果我有逗號作爲千位分隔符和小數點分隔符? 不知何故,AS3不喜歡這種正則表達式格式 – Martin 2009-08-31 15:29:23
Martin,沒有AS3正則表達式格式的使用經驗,但如果你放棄像s /和g +這樣的邊緣,那麼就拿這個匹配的結果。 – 2009-08-31 15:32:34
@Chris Ballance:是的,你的正則表達式語法是關閉的;檢查我的編輯。 – 2009-09-01 07:19:50