1
我試圖刪除字符串中的美元值。比如我有一個字符串:val str = "Hello the cost is $323,999 and it has 3 modes 1,2, and 3"
斯卡拉正則表達式替換
我基本上想要得到的輸出:"Hello the cost is $323999 and it has 3 modes 1,2, and 3"
我用正則表達式:
val pattern = """\$([0-9]+(?:,[0-9]+)*)""".r
val replacedStr = pattern replaceAllIn (str, m => m.group(1).replace(",", ""))
的問題是,由於$3
在正則表達式匹配,斯卡拉試圖在正則表達式匹配中找到第3組,並給我java.lang.IndexOutOfBoundsException: No group 3
如何擺脫此問題?
這很奇怪。我剛試過這個例子,它工作正常。 – d0nut
http://goo.gl/9J87pA這是我嘗試的工作示例。 – d0nut