gpt4 book ai didi

java - 使用正则表达式删除 MS Word 链接

转载 作者:行者123 更新时间:2023-11-30 07:58:57 28 4
gpt4 key购买 nike

我正在解析 MS Word 文档并使用 Apache POI 获取文本。

对于如下所示的段落:

The most popular fruits were apples and bananas (see section ‘Common fruits’ and subsection ‘Detailed botanic descriptions’ below).

我得到一个如下所示的字符串:

最受欢迎的水果是苹果和香蕉(请参阅“\u0013 HYPERLINK\\l“_Common_fruit_types\”\u0001\u0014Common水果\u0015”部分和“\u0013 HYPERLINK\\l\”_Botanic_description\”小节\u0001\u0014详细的植物描述\u0015'如下)。

也有不同类型的标签或关键字使用“PAGEREF”而不是“HYPERLINK”,但它们似乎总是遵循模式\u0013 TAGWORD {String1}\u0001\u0014{String2}\u0015

所以我想要做的是删除除 {String2} 之外的所有内容。到目前为止我已经做了:

  1. RegEx 模式 \u0013(.*?)\u0014 - 结果:{String2}\u0015 (从 SO 页面获取此内容,我无法找不到了)

  2. 正则表达式 \\[A-Za-z0-9]+ 删除最后的 \u0015 - 什么也没发生。我想表达的是,删除单词(包含字符和数字),包括它后面的反斜杠。还尝试了 \\\\[A-Za-z0-9]+,结果相同。

  3. 正则表达式模式\u0013(.*?)u0015删除整个链接结构

  4. 由于 \u0013(.*?)\u0014(.*?)\u0015 执行相同的操作(删除所有内容),我尝试了 \u0013(.*?)\u0014[^(.*?)]\u0015,但它什么也没做。

替代方案:While循环

boolean textWasChanged = true;
while (textWasChanged) {
int idx1 = text.indexOf("\u0013");
int idx2 = text.indexOf("\u0014", idx1);
if (idx1 > -1 && idx2 > -1 && text.replace(text.substring(idx1, idx2+1), "").length() < text.length()) {
textWasChanged = true;
text = text.replace(text.substring(idx1, idx2+1), "");
} else {
textWasChanged = false;
}

}
text = text.replaceAll("\u0015", "");

手动删除是有效的,但我想知道是否可以将其简化为一行或其他内容。

或更具体:

  1. 如何编写仅保留 {String2} 的正则表达式模式?从正则表达式手册来看,它看起来是可能的。我就是无法理解它。
  2. 我在第 2 步和/或第 4 步中的错误在哪里?我只是否定了 (.*?) 部分,因为这就是我想保留的内容。但我显然不明白正则表达式。

最佳答案

您可以使用以下模式来替换您的实体:

String raw = "The most popular fruits were apples and bananas "
+ "(see section ‘\\u0013 HYPERLINK \\l \"_Common_fruit_types\\\" "
+ "\\u0001\\u0014Common fruits\\u0015’ and subsection ‘\\u0013 HYPERLINK \\l"
+ "\\\"_Botanic_description\\\" "
+ "\\u0001\\u0014Detailed botanic descriptions\\u0015’ below).";

// test
System.out.printf("Raw string: %s%n%n", raw);
// | escaped back slash
// | | escaped unicode point
// | | | any 1+ character, reluctant
// | | | | escaped \ and unicode point
// | | | | | group 1: your goal
// | | | | | | escaped final \ + unicode point
Pattern p = Pattern.compile("\\\\u0013.+?\\\\u0014(.+?)\\\\u0015");
Matcher m = p.matcher(raw);
while (m.find()) {
System.out.printf("Found: %s%n", m.group(1));
}
System.out.println();

// actual replacement
System.out.printf(
"Replaced: %s%n",
raw.replaceAll("\\\\u0013.+?\\\\u0014(.+?)\\\\u0015", "$1")
);

输出(为了清晰起见,人为添加换行符)

Raw string: The most popular fruits were apples and bananas (see section 
‘\u0013 HYPERLINK \l "_Common_fruit_types\" \u0001\u0014Common fruits\u0015’
and subsection ‘\u0013 HYPERLINK \l\"_Botanic_description\"
\u0001\u0014Detailed botanic descriptions\u0015’ below).

Found: Common fruits
Found: Detailed botanic descriptions

Replaced: The most popular fruits were apples and bananas
(see section ‘Common fruits’ and subsection ‘Detailed botanic descriptions’ below).

关于java - 使用正则表达式删除 MS Word 链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32225929/

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