java - 使用java删除另一个双引号内的双引号

标签 java regex string replace double-quotes

我有一个字符串,其中包含另一个双引号内的双引号。

例如:

输入1:

<span style="font-family: pp-sans-big-light, "Noto Sans", Calibri, Trebuchet, Arial, "sans serif"; font-size: 17px; text-align: start; background-color: rgb(255, 255, 255);" class="transaction" name="details"> How are you</span>

预期输出1:

<span style="font-family: pp-sans-big-light, Noto Sans, Calibri, Trebuchet, Arial, sans serif; font-size: 17px; text-align: start; background-color: rgb(255, 255, 255);" class="transaction" name="details"> How are you</span>

输入2:

<span title="Conditional (A/B) Content on "Transactions.Recipient Name"" class="transaction" name="details"> Transaction Recipient</span>

预期输出 2:

<span title="Conditional (A/B) Content on Transactions.Recipient Name" class="transaction" name="details"> Transaction Recipient</span>

我尝试了以下选项,

选项 1:

public static void main(String[] args) throws Exception{
        int i;
        String title = null, style = null, temp = null;
        String tempNodeValue = "<?xml version=\"1.0\"?><dummyroot>+/**INPUT_HERE**/+</dummyroot>";
//        tempNodeValue = tempNodeValue.replace("\"","&quot;");
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document document = db.parse(new InputSource(new StringReader(tempNodeValue)));
        NodeList nodeList = document.getElementsByTagName("span");
        for(i=0;i<nodeList.getLength(); i++){
            Node node =nodeList.item(i);
            if(node.getAttributes().getNamedItem("title") != null){
                title = node.getAttributes().getNamedItem("title").getNodeValue();
                temp = title.replace("\"","'");
                tempNodeValue = tempNodeValue.replace("&quot;","\"");
                tempNodeValue = tempNodeValue.replace(title,temp);

            }
            if(node.getAttributes().getNamedItem("style") != null){
                style = node.getAttributes().getNamedItem("style").getNodeValue();
                temp = style.replace("\"","'");
                tempNodeValue = tempNodeValue.replace("&quot;","\"");
                tempNodeValue = tempNodeValue.replace(style,temp);
            }
        }
        System.out.println(tempNodeValue);

    }

选项 2:

public static void main(String[] args) throws Exception{
        String tempNodeValue = /**INPUT_HERE**/;
        tempNodeValue = tempNodeValue.replaceAll("\"(\\b[^\"]+|\\s+)?\"(\\b[^\"]+\\b)?\"([^\"]+\\b|\\s+)?\"","\"$1$2$3\"");
        System.out.println(tempNodeValue);
    }

我也尝试过 jsoup。但它们都不起作用。选项 2 适用于输入 2,但不适用于输入 1。选项 1 也不起作用。有人可以帮我解决这个问题吗?我浏览了 stackoverflow 中的所有现有答案,但没有一个有帮助。

最佳答案

**已更新

我的旧答案不起作用,但这是一个有趣的问题,我想我已经找到了解决方案。

因此,首先确定您想要的引号的开头和结尾。这个正则表达式可以做到这一点:

 ">|\"? [a-z]+="

如果您对此正则表达式进行拆分,则结果字符串中的任何引号都是不必要的。

 let originalString = "<span title="Conditional (A/B) Content on "Transactions.Recipient Name"" class="transaction" name="details"> Transaction Recipient</span>";
 originalString.split(/">|\"? [a-z]+="/)

产量

 let attributeContents = [
      "<span",
      "Conditional (A/B) Content on \"Transactions.Recipient Name\"",
      "transaction",
      "details",
      " Transaction Recipient</span>"
 ];

现在,您所要做的就是循环遍历这些子字符串,如果它们有引号,则将带引号的字符串替换为原始中不带引号的字符串。

 for(let index in attributeContents) {
      let attributeValue = attributeContents[index];
      originalString = originalString.replace(attributeValue, attributeValue.replace(/"/g, "");
 }
 // double comments have now been removed from the original string.

关于java - 使用java删除另一个双引号内的双引号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57996432/

相关文章:

java - 什么是 NullPointerException,我该如何解决?

regex - 正则表达式匹配所有内容,直到最后一次出现/

python - Pandas RegEx 提取子字符串上的第一个匹配项,传递的项目数错误

c - 在字符串中添加一个字符并打印它会在 C 中打印乱码

java - ICEFaces AJAX 重定向到 404

java - 如何正确部署 Spring Cloud 服务到 AWS ECS

java - 在 Java 和 Android 中通过套接字获取服务器响应

辅音和元音的Python正则表达式

javascript - 从字符串中匹配并选择日期?

python - 如何用 Python 中的另一个字符替换字符串中的一个字符?