Java 选择包含斜杠的文本的Jsoup元素
我试图用下面的代码选择一个标记,它在自己的文本中包含斜杠Java 选择包含斜杠的文本的Jsoup元素,java,jsoup,Java,Jsoup,我试图用下面的代码选择一个标记,它在自己的文本中包含斜杠 Document testDocument = Jsoup.parse("<p>wanted \\ text test</p>"); Elements els = testDocument.select("p:containsOwn(wanted \\)"); Elements els2 = testDocument.select("p:contains(wanted \\)"); documenttestdoc
Document testDocument = Jsoup.parse("<p>wanted \\ text test</p>");
Elements els = testDocument.select("p:containsOwn(wanted \\)");
Elements els2 = testDocument.select("p:contains(wanted \\)");
documenttestdocument=Jsoup.parse(“wanted\\text test”;
Elements els=testDocument.select(“p:containsOwn(wanted\\)”;
Elements els2=testDocument.select(“p:contains(wanted\\)”;
当我使用
p:contains(wanted)
时,会返回p标记,但当我在末尾添加斜杠时,不会返回任何内容。实际上,这里是在Java代码中定义testDocument
Document testDocument = Jsoup.parse("<p>wanted \\ text test</p>");//if you print this, <p>wanted \\ text test</p>
documenttestdocument=Jsoup.parse(“wanted\\text test”//如果打印此文件,需要\\文本测试
因此\n您使用的第一个反斜杠用于转义,另一个是该字符串中的实际反斜杠wanted\text test
但是,如果要从文件中重新生成此所需\\text test
,则Java会假定它们是普通字符,因为特殊字符已经转换
所以在这里,当您尝试查找testDocument.select(“p:contains(wanted\)”)时,实际上不会显示任何文本
因此,最好将反斜杠替换为其他字符并获取文本:
Document testDocument = Jsoup.parse("<p>wanted \\ text test</p>".replace("\\", "/"));
Elements els = testDocument.select("p:containsOwn(wanted \\)");
String x = "wanted /";
Elements els2 = testDocument.select("p:contains("+x+")");
System.out.println(els);
System.out.println(els2);
documenttestdocument=Jsoup.parse(“wanted\\text test”。替换(“\\”,“/”);
Elements els=testDocument.select(“p:containsOwn(wanted\\)”;
字符串x=“通缉/”;
Elements els2=testDocument.select(“p:contains(+x+)”);
系统输出打印项次(els);
系统输出打印项次(els2);