gpt4 book ai didi

regex - Scala 中占位符替换正则表达式的语法是什么?

转载 作者:行者123 更新时间:2023-12-01 11:03:37 25 4
gpt4 key购买 nike

所以我可以写:

val res = """(\d*).*""".r.replaceAllIn(s,"""\1""")

到目前为止,我得到的是文字“1”而不是捕获的组。

提前谢谢你。

最佳答案

试试这个:$1 而不是 \1

Scala 的 Regex 在底层使用 java.util.regex(至少在 JVM 上)。现在,如果您在 Java 文档中查找 replaceAll,您将看到:

Note that backslashes () and dollar signs ($) in the replacement string may cause the results to be different than if it were being treated as a literal replacement string. Dollar signs may be treated as references to captured subsequences as described above, and backslashes are used to escape literal characters in the replacement string.

关于regex - Scala 中占位符替换正则表达式的语法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8288787/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com