我需要替换以下字符串中的引号。
String str = "This is 'test()ing' and test()ing'";
最终输出应该是“This is test()ing and test()ing'”;
即仅当它以 'test() 开头并以 '.中间的文本保持不变。
这个不行。
str = str.replaceAll("^('test())(.*)(')$", "test()$2");
请为此建议合适的正则表达式。
最佳答案
这个正则表达式会得到想要的结果:
str = str.replaceAll("'test\\(\\)(.*?)'", "test()$1");
对于 .*?
字符串是惰性匹配的,不会匹配到整个字符串的末尾。
https://stackoverflow.com/questions/28637346/
相关文章:
asp.net-mvc - Razor Html.Raw 无法呈现 ViewModel 数据
python-2.7 - 如何使用python检查机器人框架中的字典是否为空
python - 使用 np.arrays python 对列表进行排序
oracle - 增加 SQL Developer 导出中的行数
python - 断言错误 : View function mapping is overwriti
angularjs - 嵌套的 ng-repeat 中的 Angular ng-bind-html
angularjs - 作为指令的表行在 angularjs 中显示在表上下文之外