gpt4 book ai didi

Java String.replaceAll 正则表达式

转载 作者:搜寻专家 更新时间:2023-11-01 04:07:04 24 4
gpt4 key购买 nike

我想替换

的第一个上下文

web/style/clients.html

使用 java String.replaceFirst 方法,我可以获得:

${pageContext.request.contextPath}/style/clients.html

我试过了

String test =  "web/style/clients.html".replaceFirst("^.*?/", "hello/");

这给了我:

hello/style/clients.html

但是当我这样做的时候

 String test =  "web/style/clients.html".replaceFirst("^.*?/", "${pageContext.request.contextPath}/");

给我

java.lang.IllegalArgumentException: 非法组引用

最佳答案

我的预感是它正在爆炸,因为 $ 是一个特殊字符。来自 the documentation

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.

所以我相信你会需要类似的东西

"\\${pageContext.request.contextPath}/"

关于Java String.replaceAll 正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2602133/

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