匹配Java字符串中的同时标记
我正在编写一个Java程序,其中有些数据必须从字符串中提取(实际上这是html) 我的代码如下:匹配Java字符串中的同时标记,java,regex,Java,Regex,我正在编写一个Java程序,其中有些数据必须从字符串中提取(实际上这是html) 我的代码如下: while ((line = in.readLine()) != null) { if (line.contains("xrefInternal")) { String ftnNum = line.replaceAll("(.*)(<sup>)([0-9]+)(</sup>)(.*)", "$3");
while ((line = in.readLine()) != null) {
if (line.contains("xrefInternal")) {
String ftnNum = line.replaceAll("(.*)(<sup>)([0-9]+)(</sup>)(.*)", "$3");
String ftnRefNum = line.replaceAll("(.*)(<span class=\"xrefInternal\" id=\"fo)([0-9]+)(\")(.*)", "$3");
System.out.println(ftnRefNum + "\t" + ftnNum);
}
}
这里有工作
请告诉我如何重新编写代码,以便案例1
的功能与案例2
谢谢您描述的行为不是正则表达式的结果,无法使用提供的代码复制。
(如果/当提供更多信息时,我将更新/删除此内容。此内容太长,无法发表评论,将有助于旗手。)
我得到(文本代表示例编号):
当我运行此命令时:
String example1="<p class=\"paraNoIndent1\" style=\"text-indent: 0%;\">texy<span class=\"xrefInternal\" id=\"fo249\"><a href=\"abc.html#fo_249\"><sup>2</sup></a></span> Tewxt.<span class=\"xrefInternal\" id=\"fo250\"><a href=\"abc.html#fo_250\"><sup>3</sup></a></span> text</p>";
String ftnNum = example1.replaceAll("(.*)(<sup>)([0-9]+)(</sup>)(.*)", "$3");
String ftnRefNum = example1.replaceAll("(.*)(<span class=\"xrefInternal\" id=\"fo)([0-9]+)(\")(.*)", "$3");
System.out.println(ftnRefNum + " one " + ftnNum);
String example2="<p class=\"paraNoIndent1\" style=\"text-indent: 0%;\">Text.<span class=\"xrefInternal\" id=\"fo248\"><a href=\"abc.html#fo_248\"><sup>1</sup></a></span></p>";
String ftnNum2 = example2.replaceAll("(.*)(<sup>)([0-9]+)(</sup>)(.*)", "$3");
String ftnRefNum2 = example2.replaceAll("(.*)(<span class=\"xrefInternal\" id=\"fo)([0-9]+)(\")(.*)", "$3");
System.out.println(ftnRefNum2 + " two " + ftnNum2);
String example1=“texy-Tewxt.text
”;
字符串ftnNum=example1.replaceAll((.*)()([0-9]+)()(.*),“$3”);
String ftnRefNum=example1.replaceAll((*)(强制链接:(常规)(更具体)。这就是为什么不使用正则表达式来解析XML或HTML。XML和HTML不是常规语言,通常不能用正则表达式解析,除非在非常有限的情况下。请使用真正的HTML或XML解析器。
<p class="paraNoIndent1" style="text-indent: 0%;">Text.<span class="xrefInternal" id="fo248"><a href="abc.html#fo_248"><sup>1</sup></a></span></p>
248 1
250 one 3
248 two 1
String example1="<p class=\"paraNoIndent1\" style=\"text-indent: 0%;\">texy<span class=\"xrefInternal\" id=\"fo249\"><a href=\"abc.html#fo_249\"><sup>2</sup></a></span> Tewxt.<span class=\"xrefInternal\" id=\"fo250\"><a href=\"abc.html#fo_250\"><sup>3</sup></a></span> text</p>";
String ftnNum = example1.replaceAll("(.*)(<sup>)([0-9]+)(</sup>)(.*)", "$3");
String ftnRefNum = example1.replaceAll("(.*)(<span class=\"xrefInternal\" id=\"fo)([0-9]+)(\")(.*)", "$3");
System.out.println(ftnRefNum + " one " + ftnNum);
String example2="<p class=\"paraNoIndent1\" style=\"text-indent: 0%;\">Text.<span class=\"xrefInternal\" id=\"fo248\"><a href=\"abc.html#fo_248\"><sup>1</sup></a></span></p>";
String ftnNum2 = example2.replaceAll("(.*)(<sup>)([0-9]+)(</sup>)(.*)", "$3");
String ftnRefNum2 = example2.replaceAll("(.*)(<span class=\"xrefInternal\" id=\"fo)([0-9]+)(\")(.*)", "$3");
System.out.println(ftnRefNum2 + " two " + ftnNum2);