Scala regex replace with anonymous function

前端 未结 1 1272
星月不相逢
星月不相逢 2021-02-13 22:38

In Ruby I can replace characters in a string in the following way:

a = \"one1two2three\"
a.gsub(/\\d+/) {|e| e.to_i + 1}
=> \"one2two3three\"
<
相关标签:
1条回答
  • 2021-02-13 23:24

    Yes, Regex#replaceAllIn has an overloaded version that takes a function Match => String. The equivalent Scala version of your code would be:

    """\d+""".r.replaceAllIn("one1two2three", m => (m.group(0).toInt + 1).toString)
    
    0 讨论(0)
提交回复
热议问题