Java replaceAll有限制

时间:2016-09-29 10:03:20

标签: java regex

我有一个文本,例如

"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

3 个答案:

答案 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}}