Java regex - 用没有分隔符的相同子字符串替换分隔符之间的子字符串 [英] Java regex - replace substrings between delimiters with same substrings without delimiters

查看:34
本文介绍了Java regex - 用没有分隔符的相同子字符串替换分隔符之间的子字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想弄清楚如何在像这样的字符串中用 Java 1.6 替换

I'm trying to figure out how to replace with Java 1.6 in strings like

hello ${world }!   ${txt + '_t'}<br/> ${do_not_replace

'${' 和 '}' 之间标识的任何子字符串具有相同的子字符串,没有这些分隔符.所以上面字符串的输出应该是

any substring identified between '${' and '}' with the same substring without these delimiters. So the output for the string above should be

hello world !   txt + '_t'<br/> ${do_not_replace

我确定了一个允许我用固定字符串替换子字符串的工作模式

I identified a working pattern that allows me to replace the substrings with a fixed string

str.replaceAll('[${](.*?)}', '_')

而且我知道我不能在这个版本的 Java 中使用命名组.

and i know that i cannot use named groups with this version of Java.

任何有关此问题的简单解决方案的建议都非常感谢!非常感谢

Any suggestion for a simple solution to this problem are highly appreciated! Many thanks

推荐答案

try

    s = s.replaceAll("\\$\\{(.+?)}", "$1");

这篇关于Java regex - 用没有分隔符的相同子字符串替换分隔符之间的子字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆