gpt4 book ai didi

java - 匹配器。 appendReplacement 未添加起始内容

转载 作者:行者123 更新时间:2023-12-01 13:48:07 27 4
gpt4 key购买 nike

public class TestUtil {


public static void main(String[] args) {
StringBuffer test = new StringBuffer();
test.append("abacbsidfslhfadskljfhdskh adsfkjlhdslkfhas lkajdsfhak dsjfhs akhasdf adsjkfh asldjkfhds glakdshgf dghkads ghklgh asdflkghadfkl <p rendition=\"#indent-1\">1. Geschl. <hi rendition=\"#r\"> <hi rendition=\"#smcap\"> <hi rendition=\"#wide\"><term xml:lang=\"la\">Homo</term></hi> </hi>. <term xml:lang=\"la\">Erectus</term>, <term xml:lang=\"la\">bimanus</term>. Mentoprominulo. Dentibus aequaliter approximatis; incisoribus inferioribus erectis.</hi> </p>");
test.append("bbbbbbbbbbbbbbbbbbbbbb <p rendition=\"#indent-1\">1. Geschl. <hi rendition=\"#r\"> <hi rendition=\"#smcap\"> <hi rendition=\"#wide\"><term xml:lang=\"la\">sHomo</term></hi> </hi>. <term xml:lang=\"la\">sErectus</term>, <term xml:lang=\"la\">sbimanus</term>. Mentoprominulo. Dentibus aequaliter approximatis; incisoribus inferioribus erectis.</hi> </p>");
Pattern pattern = Pattern.compile("<p rendition=\"#indent-1\">\\d+\\.\\s*.*?</p>",
Pattern.CASE_INSENSITIVE);
Matcher regexMatcher = pattern.matcher(test.toString());
System.out.println(test);
test.delete(0, test.length());
while (regexMatcher.find()) {

// test.delete(regexMatcher.start(),test.length());
String matched =regexMatcher.group(0);
Pattern termPatter=Pattern.compile("(<term xml:lang=\".*?\")(>)(.*?)(</term>)");

Matcher termMatcher = termPatter.matcher(matched);

if(termMatcher != null){
//termMatcher.start();
System.out.println(termMatcher.groupCount());
while (termMatcher.find()) {
System.out.println("0---"+termMatcher.group(0));
System.out.println(termMatcher.group(1));
System.out.println(termMatcher.group(2));
System.out.println(termMatcher.group(3));
System.out.println(termMatcher.group(4));

termMatcher.appendReplacement(test, appendSortKey(termMatcher.group(0),termMatcher.group(1),termMatcher.group(2),termMatcher.group(3),termMatcher.group(4)));

}
termMatcher.appendTail(test);
}
//regexMatcher.appendTail(test);
}
System.out.println(test);
}

private static String appendSortKey(String totStr, String termStart, String termStartEndTag, String termValue, String termEndTag) {
// TODO Auto-generated method stub
if(totStr!=null){
termStart = termStart+" "+"sortKey=\""+termValue+"\""+termStartEndTag;
return termStart+termValue+termEndTag;
}

return null;
}
}

尝试仅通过从另一个正则表达式的匹配器获取内容(因为它是条件)来操作 < term>..... 但在开始和结束时丢失了内容,请让我知道我的错误我正在做。

预期输出是

abacbsidfslhfadskljfhdskh adsfkjlhdslkfhas lkajdsfhak dsjfhs akhasdf adsjkfh asldjkfhds glakdshgf dghkads ghklgh asdflkghadfkl <p rendition="#indent-1">1. Geschl. <hi rendition="#r"> <hi rendition="#smcap"> <hi rendition="#wide"><term xml:lang="la" sortKey="Homo">Homo</term></hi> </hi>. <term xml:lang="la" sortKey="Erectus">Erectus</term>, <term xml:lang="la"sortKey="bimanus" >bimanus</term>. Mentoprominulo. Dentibus aequaliter approximatis; incisoribus inferioribus erectis.</hi> </p>bbbbbbbbbbbbbbbbbbbbbb <p rendition="#indent-1">1. Geschl. <hi rendition="#r"> <hi rendition="#smcap"> <hi rendition="#wide">><term xml:lang="la" sortKey="sHomo">sHomo</term></hi> </hi>. <term xml:lang="la" sortKey="sErectus">sErectus</term>, <term xml:lang="la"sortKey="sbimanus" >sbimanus</term>. Mentoprominulo. Dentibus aequaliter approximatis; incisoribus inferioribus erectis.</hi> </p>

最佳答案

您更改以下代码行,它将给出正确的结果。

Pattern pattern = Pattern.compile(".*<p rendition=\"#indent-1\">\\d+\\.\\s*.*?</p>",
Pattern.CASE_INSENSITIVE);
/*
instead of the following
Pattern pattern = Pattern.compile("<p rendition=\"#indent-1\">\\d+\\.\\s*.*?</p>",
Pattern.CASE_INSENSITIVE);
*/

说明:

  • <p rendition=\"#indent-1\">\\d+\\.\\s*.*?</p>部分匹配<p ...> ... </p>部分,因此 appendReplacement 仅附加 <p ...> ... </p>部分替换。
  • .*<p rendition=\"#indent-1\">\\d+\\.\\s*.*?</p>部分将匹配 Text <p ...> ... </p> ,因此在 appendReplacement 之后,您将得到 Text <p ...> ... </p>与替代品。

因此输出将是整个字符串 <term xml:lang="la">text</term>被替换为<term xml:lang="la" sortKey="text">text</term>

关于java - 匹配器。 appendReplacement 未添加起始内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20186060/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com