我有一个文本,例如
"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("\"", "\\\"");
但我有一个限制:
我可以使用后续步骤解决它:split string,replace,concatenate ... 我知道它也可以使用正则表达式来解决 - 更好的方法。
但不幸的是我无法建立具有限制的对应模式......
结果我的例子应该是( “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
答案 0 :(得分:1)
我认为你想要的是以下(使用外观):
public function register(Request $request)
{
// This runs the validate method in the action.
$this->validator($request->all())->validate();
event(new Registered($user = $this->create($request->all())));
$this->guard()->login($user);
return redirect($this->redirectPath());
}
示例强>
// | 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("(?<!^|:|,)\"(?!:|,|$)", "\\\\\"")
);
注意强>
有关环视的其他见解,请参阅Java模式doc page。
答案 1 :(得分:1)
你可以使用负面的lookeahed和lookbehind。
public static void main(String[] args) {
String s = "\"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";
String p = s.replaceAll("(?<![,:])\"(?![,:])", "\\\"");
String expected = "\"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(p.equals(expected));
}
O / P:
true
答案 2 :(得分:0)
试试这个:{{1}}