我有一个字符串,其中包含另一个双引号内的双引号。
例如:
输入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("\"",""");
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(""","\"");
tempNodeValue = tempNodeValue.replace(title,temp);
}
if(node.getAttributes().getNamedItem("style") != null){
style = node.getAttributes().getNamedItem("style").getNodeValue();
temp = style.replace("\"","'");
tempNodeValue = tempNodeValue.replace(""","\"");
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/