Java 删除带有正则表达式的MS Word链接
我正在解析MS Word文档,并使用ApachePOI获取文本 对于这样的段落: 最受欢迎的水果是苹果和香蕉(见下文第“”节和第“”小节) 我得到一条如下所示的字符串:Java 删除带有正则表达式的MS Word链接,java,regex,Java,Regex,我正在解析MS Word文档,并使用ApachePOI获取文本 对于这样的段落: 最受欢迎的水果是苹果和香蕉(见下文第“”节和第“”小节) 我得到一条如下所示的字符串: 最受欢迎的水果是苹果和香蕉(见下文“\u0013 HYPERLINK\\l”\u0001\u0014常见水果\u0015”和“\u0013 HYPERLINK\\l\”\u0001\u0014详细植物描述\u0015”小节)。 也有不同类型的标记或关键字使用“PAGEREF”而不是“HYPERLINK”,但它们似乎总是遵循模式\
最受欢迎的水果是苹果和香蕉(见下文“\u0013 HYPERLINK\\l”\u0001\u0014常见水果\u0015”和“\u0013 HYPERLINK\\l\”\u0001\u0014详细植物描述\u0015”小节)。
也有不同类型的标记或关键字使用“PAGEREF”而不是“HYPERLINK”,但它们似乎总是遵循模式\u0013 TAGWORD{String1}\u0001\u0014{String2}\u0015
所以我想做的是删除所有东西,除了{String2}
。到目前为止,我已经做了:
\u0013(.*?\u0014
-结果:{String2}\u0015
(从一个SO页面获取,我再也找不到了)\[A-Za-z0-9]+
删除最终的\u0015
-没有发生任何事情。我想表达的是,删除这个单词(包含字符和数字),包括它后面的反斜杠。还尝试了\\[A-Za-z0-9]+
,结果相同\u0013(.*u0015
删除整个链接结构\u0013(.*?)\u0014(.*?\u0015
执行相同的操作(删除所有),因此我尝试了\u0013(.*?\u0014[^(.*?)\u0015
,但它没有执行任何操作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", "");
boolean textWasChanged=true;
while(textwaschange){
int idx1=text.indexOf(“\u0013”);
int idx2=text.indexOf(“\u0014”,idx1);
if(idx1>-1&&idx2>-1&&text.replace(text.substring(idx1,idx2+1)).length()
手动移除是可行的,但我想知道它是否可以简化为一个衬里或其他东西
或者更具体地说:
{String2}
的正则表达式模式?从regex手册来看,这似乎是可能的。我就是不能把我的头绕过去(.*)
部分,这就是我想要保留的。但我显然不懂regex enoug您可以使用以下
模式
替换实体:
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")
);
输出(为清晰起见,人为添加了换行符)
非常感谢你!它工作得很好!必须将四个“\”减少到两倍,因为奇怪符号的原始字符串似乎真的是
\u0013
,而不是\\u0013
(但它是\\l
,我会马上纠正)。也非常感谢您对所使用的正则表达式模式的解释!非常有用:-)
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).