[英]A regex to return text from parsed word document
I trying to create a regex to match a portion of text in my word document. 我试图创建一个正则表达式来匹配我的Word文档中的一部分文本。 in the word document I have something like this {LigneDetails.Libelle} so when I treat this file with java it generates like this : 在word文档中,我有类似{LigneDetails.Libelle}的内容,因此当我使用Java处理此文件时,它会生成如下内容:
<w:t>{</w:t>
</w:r>
<w:proofErr w:type="spellStart" />
<w:r w:rsidRPr="009664EA">
<w:t>SOCIETE.RaisonSociale</w:t>
</w:r>
<w:proofErr w:type="spellEnd" />
<w:r w:rsidRPr="009664EA">
<w:t>}</w:t>
so here I match that text between the curved brackets using this regex : \\\\{([^\\\\{])*\\\\}
, this will return : 所以在这里,我使用以下正则表达式将文本匹配到括号之间: \\\\{([^\\\\{])*\\\\}
,这将返回:
{</w:t>
</w:r>
<w:proofErr w:type="spellStart" />
<w:r w:rsidRPr="009664EA">
<w:t>SOCIETE.RaisonSociale</w:t>
</w:r>
<w:proofErr w:type="spellEnd" />
<w:r w:rsidRPr="009664EA">
<w:t>}
Now in my word document I have something like this : {LigneDetails.Libelle:FAM:01}
现在在我的Word文档中,我有类似以下内容的内容: {LigneDetails.Libelle:FAM:01}
This will generate : 这将产生:
<w:t>{</w:t>
</w:r>
<w:proofErr w:type="spellStart" />
<w:r w:rsidRPr="002A51DD">
<w:rPr>
<w:sz w:val="14" />
<w:szCs w:val="20" />
</w:rPr>
<w:t>LigneDetails.Libelle:FAM</w:t>
</w:r>
<w:proofErr w:type="spellEnd" />
<w:r w:rsidRPr="002A51DD">
<w:rPr>
<w:sz w:val="14" />
<w:szCs w:val="20" />
</w:rPr>
<w:t>:01}</w:t>
then the regex will match the portion : 那么正则表达式将匹配以下部分:
{</w:t>
</w:r>
<w:proofErr w:type="spellStart" />
<w:r w:rsidRPr="002A51DD">
<w:rPr>
<w:sz w:val="14" />
<w:szCs w:val="20" />
</w:rPr>
<w:t>LigneDetails.Quantite:FAM</w:t>
</w:r>
<w:proofErr w:type="spellEnd" />
<w:r w:rsidRPr="002A51DD">
<w:rPr>
<w:sz w:val="14" />
<w:szCs w:val="20" />
</w:rPr>
<w:t>:01}
until now all is fine. 到现在为止一切都很好。
Now I want to match the last two values which is always come after :
, in my case that would be FAM
and 01
so I want this regex to return these two values. 现在,我想匹配总是在:
之后的最后两个值,在我的情况下,这将是FAM
和01
因此我希望此正则表达式返回这两个值。
how can I do that ? 我怎样才能做到这一点 ?
If we take into account your current approach, you are left with some {...}
strings where you either have <...>
entities or text or the {
at the start and }
at the end that you can remove with regex. 如果我们考虑到你目前的做法,你会留下一些{...}
字符串,你要么有<...>
实体或文字或{
开始和}
在您可以用正则表达式除去结束。 Then, you need to just grab the lines and split with :
, or use a regex to grab all non-whitespace chars after :
symbols. 然后,您只需要抓取行并用:
分割,或使用正则表达式来抓取:
符号后的所有非空白字符。
A sample Java code: 示例Java代码:
String str = "{</w:t>\n </w:r>\n <w:proofErr w:type=\"spellStart\" />\n <w:r w:rsidRPr=\"002A51DD\">\n <w:rPr>\n <w:sz w:val=\"14\" />\n <w:szCs w:val=\"20\" />\n </w:rPr>\n <w:t>LigneDetails.Quantite:FAM</w:t>\n </w:r>\n <w:proofErr w:type=\"spellEnd\" />\n <w:r w:rsidRPr=\"002A51DD\">\n <w:rPr>\n <w:sz w:val=\"14\" />\n <w:szCs w:val=\"20\" />\n </w:rPr>\n <w:t>:01}";
str = str.replaceAll("<[^<]*?>|^\\{|\\}$", "");
String[] lines = str.split("\n");
List<String> lst = new ArrayList<>();
for (String s : lines) {
if (s.contains(":"))
lst.add(s.trim().split(":")[1]);
}
System.out.println(lst);
Or a version with a :(\\S+)
regex grabbing 1+ non-whitespace chunks from the stripped string contents: 或带有:(\\S+)
正则表达式的版本可从剥离的字符串内容中获取1+个非空格块:
str = str.replaceAll("<[^<]*?>|^\\{|\\}$", "");
Matcher m = Pattern.compile(":(\\S+)").matcher(str);
List<String> lst = new ArrayList<>();
while (m.find()) {
lst.add(m.group(1));
}
See another demo 观看另一个演示
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.