gpt4 book ai didi

Java 的replaceAll 有限制

转载 作者:行者123 更新时间:2023-12-02 03:19:48 24 4
gpt4 key购买 nike

例如,我有一段文字

"IssueDetail":"IQR: ON THE FORM YOU MARKED YES TO THE FOLLOWING QUESTION: Anything preventing you from completing the inspection? YOU NEED TO MARK "NO" ON THIS PART. FURTHER DOWN THERE IS A QUESTION THAT STATES : Do you have interior access to complete the inspection? THIS IS WHERE YOU ARE GOING TO CHECK "NO" AND UPLOAD THE PHOTO. PLEASE COMPLETE THIS ON THE FORM AND FOLLOW THE REST OF THE PROMPTS TO COMPLETE THE FULL EXTERIOR INSPECTION. IF YOU HAVE ANY QUESTIONS PLEASE CONTACT YOUR VC. THANK YOU! ","PhotoIssue":true,"FormIssue":true,"SendEmail":true

在本文中,我需要将 " 替换为 \"

在简单的情况下,可以使用来完成

result = string.replaceAll("\"", "\\\"");

但我有一个限制:

  • " 不得在开头或结尾处替换(如果 " 是第一个或最后一个元素);
  • " 不得在指令中替换 ":"
  • " 不得在指令中替换为 ","

我可以使用后续步骤解决它:拆分字符串、替换、连接...我知道它也可以使用正则表达式来解决 - 更好的方法。

但不幸的是我无法在限制下构建对应的模式...

结果我的例子应该是(“NO” -\“NO\”):

"IssueDetail":"IQR: ON THE FORM YOU MARKED YES TO THE FOLLOWING QUESTION: Anything preventing you from completing the inspection? YOU NEED TO MARK \"NO\" ON THIS PART. FURTHER DOWN THERE IS A QUESTION THAT STATES : Do you have interior access to complete the inspection? THIS IS WHERE YOU ARE GOING TO CHECK \"NO\" AND UPLOAD THE PHOTO. PLEASE COMPLETE THIS ON THE FORM AND FOLLOW THE REST OF THE PROMPTS TO COMPLETE THE FULL EXTERIOR INSPECTION. IF YOU HAVE ANY QUESTIONS PLEASE CONTACT YOUR VC. THANK YOU! ","PhotoIssue":true,"FormIssue":true,"SendEmail":true

最佳答案

我认为您想要的是以下内容(使用环视):

//               | not preceded by beginning of string or : or ,
// | | escaped "
// | | | not followed by : or , or end of string
test.replaceAll("(?<!^|:|,)\"(?!:|,|$)", "\\\\\"")

示例

String test = "\"IssueDetail\":\"IQR: ON THE FORM YOU MARKED YES TO THE FOLLOWING QUESTION: "
+ "Anything preventing you from completing the inspection? YOU NEED TO MARK \"NO\" "
+ "ON THIS PART. FURTHER DOWN THERE IS A QUESTION THAT STATES : Do you have "
+ "interior access to complete the inspection? THIS IS WHERE YOU ARE GOING TO "
+ "CHECK \"NO\" AND UPLOAD THE PHOTO. PLEASE COMPLETE THIS ON THE FORM AND "
+ "FOLLOW THE REST OF THE PROMPTS TO COMPLETE THE FULL EXTERIOR INSPECTION. "
+ "IF YOU HAVE ANY QUESTIONS PLEASE CONTACT YOUR VC. THANK YOU! "
+ "\",\"PhotoIssue\":true,\"FormIssue\":true,\"SendEmail\":true";

System.out.println(
test.replaceAll("(?<!^|:|,)\"(?!:|,|$)", "\\\\\"")
);

输出(为了清晰起见,添加了换行符)

"IssueDetail":"IQR: ON THE FORM YOU MARKED YES TO THE FOLLOWING QUESTION: 
Anything preventing you from completing the inspection? YOU NEED TO MARK \"NO\"
ON THIS PART. FURTHER DOWN THERE IS A QUESTION THAT STATES : Do you have interior
access to complete the inspection? THIS IS WHERE YOU ARE GOING TO CHECK \"NO\"
AND UPLOAD THE PHOTO. PLEASE COMPLETE THIS ON THE FORM AND FOLLOW THE REST OF
THE PROMPTS TO COMPLETE THE FULL EXTERIOR INSPECTION. IF YOU HAVE ANY QUESTIONS
PLEASE CONTACT YOUR VC. THANK YOU! ","PhotoIssue":true,"FormIssue":true,
"SendEmail":true

注意

查看 Java 模式 doc page有关环视的更多见解。

关于Java 的replaceAll 有限制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39767276/

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