我需要替换以下字符串中的引号。
String str = "This is 'test()ing' and test()ing'";
Run Code Online (Sandbox Code Playgroud)
最终输出应该是“This is test()ing and test()ing'”;
即仅当它以 'test() 开头并以 ' 结尾时才替换。中间的文字保持不变。
这个不行。
str = str.replaceAll("^('test())(.*)(')$", "test()$2");
Run Code Online (Sandbox Code Playgroud)
请建议合适的正则表达式。
此正则表达式将具有所需的结果:
str = str.replaceAll("'test\\(\\)(.*?)'", "test()$1");
Run Code Online (Sandbox Code Playgroud)
与.*?字符串延迟匹配并且不匹配到整个字符串的末尾。