gpt4 book ai didi

java - 将 ' (single quote once) but not ' '(单引号两次)替换为 '||' '' 的正则表达式

转载 作者:搜寻专家 更新时间:2023-10-31 19:32:04 26 4
gpt4 key购买 nike

在下一行中,如果我们有单引号 ('),那么我们必须将其替换为 '||''',但如果我们有两次单引号 (''),那么它应该保持原样。

我尝试了下面的一段代码,但没有给我正确的输出。

代码片段:

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/

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