在下一行中,如果我们有单引号 ('),那么我们必须将其替换为 '||''',但如果我们有两次单引号 (''),那么它应该保持原样。
我尝试了下面的一段代码,但没有给我正确的输出。
代码片段:
static String replaceSingleQuoteOnly = "('(?!')'{0})";
String line2 = "Your Form xyz doesn't show the same child''s name as the name in your account with us.";
System.out.println(line2.replaceAll(replaceSingleQuoteOnly, "'||'''"));
以上代码的实际输出:
Your Form xyz doesn'||'''t show the same child''||'''s name as the name in your account with us.
预期结果:
Your Form xyz doesn'||'''t show the same child''s name as the name in your account with us.
Regular expression is replacing child''s with child''||'''s. child''s should remain as it is.
最佳答案
您可以使用 lookarounds为此,例如:
String replaceSingleQuoteOnly = "(?<!')'(?!')";
String line2 = "Your Form xyz doesn't show the same child''s name as the name in your account with us.";
System.out.println(line2.replaceAll(replaceSingleQuoteOnly, "'||'''"));
关于java - 将 ' (single quote once) but not ' '(单引号两次)替换为 '||' '' 的正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42877489/