如何仅在 Scala 中小写捕获组
How to lowercase the capture group only in scala
此代码:
"""{"nAMe": "Deloise", "WINS": [["three of a kind", "5♣"]]}""".replaceAll("""(\"[^"]+\" *:)""", "|UPERCASETEST||".toLowerCase())
产生:
String = {|upercasetest|"nAMe":| "Deloise", |upercasetest|"WINS":| [["three of a kind", "5♣"]]}
当我期待的时候:
String = {|upercasetest|"name":| "Deloise", |upercasetest|"wins":| [["three of a kind", "5♣"]]}
知道为什么捕获组不希望小写以及如何解决它吗?
您需要将匹配对象传递给 replaceAllIn
中的 lambda 表达式,您可以在其中操作内容,否则,在 replaceAll
中,</code> 不会得到 "expanded" 到实际的第 1 组子匹配值:</p>
<pre><code>val s = """{"nAMe": "Deloise", "WINS": [["three of a kind", "5♣"]]}"""
val rx = """(\"[^"]+\" *:)""".r
val replacedStr = rx replaceAllIn (s, m => s"|UPERCASETEST|${m.group(1)}|".toLowerCase())
println(replacedStr)
输出:
{|upercasetest|"name":| "Deloise", |upercasetest|"wins":| [["three of a kind", "5♣"]]}
此代码:
"""{"nAMe": "Deloise", "WINS": [["three of a kind", "5♣"]]}""".replaceAll("""(\"[^"]+\" *:)""", "|UPERCASETEST||".toLowerCase())
产生:
String = {|upercasetest|"nAMe":| "Deloise", |upercasetest|"WINS":| [["three of a kind", "5♣"]]}
当我期待的时候:
String = {|upercasetest|"name":| "Deloise", |upercasetest|"wins":| [["three of a kind", "5♣"]]}
知道为什么捕获组不希望小写以及如何解决它吗?
您需要将匹配对象传递给 replaceAllIn
中的 lambda 表达式,您可以在其中操作内容,否则,在 replaceAll
中,</code> 不会得到 "expanded" 到实际的第 1 组子匹配值:</p>
<pre><code>val s = """{"nAMe": "Deloise", "WINS": [["three of a kind", "5♣"]]}"""
val rx = """(\"[^"]+\" *:)""".r
val replacedStr = rx replaceAllIn (s, m => s"|UPERCASETEST|${m.group(1)}|".toLowerCase())
println(replacedStr)
输出:
{|upercasetest|"name":| "Deloise", |upercasetest|"wins":| [["three of a kind", "5♣"]]}