我在 Ruby 中的字串包含貨幣和金額。
我已經從網上商店 123X87 購買了價值 500.80 美元的商品
我期待以下結果
我已經從網上商店 123X87 購買了價值 500.80 美元的商品
我嘗試過使用 Regex 但沒有成功。
str = "I have purchased and item at USD500.80 from online store store 123X87"
str.gsub(/\d /, ' \0 ')
# wrong result
"I have purchased and item at USD 500 . 80 from online store store 123 X 87 "
我不確定該字串包含什么貨幣。我只知道貨幣和金額沒有空格。
str = "I have purchased and item at EUR500.80 from online store store 123X88"
str = "I have purchased and item at GBP500.80 from online store store 123X88"
uj5u.com熱心網友回復:
我會更改正則運算式以明確掃描所有已知的貨幣代碼。任何過度概括的正則運算式(例如\w \d ,例如)都必然會破壞字串的其他部分,例如訂單號。
str = "I have purchased and item at USD500.80 from online store store 123X87"
str.gsub(/(USD|EUR)(\d )/, '\1 \2')
# => "I have purchased and item at USD 500.80 from online store store 123X87"
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/364002.html
