【发布时间】:2016-10-07 09:57:39
【问题描述】:
我正在尝试在 xml 标记之间提取文本。标签之间的文本是多语言的。 例如:
<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">
तुम्हारा नाम क्या है
</string>
我尝试用谷歌搜索它并得到了一些正则表达式,但没有奏效 这是我尝试过的一个:
String str = "<string xmlns="+
"http://schemas.microsoft.com/2003/10/Serialization/"+">"+
"तुम्हारा नाम क्या है"+"</string>";
final Pattern pattern = Pattern.compile("<String xmlns="+
"http://schemas.microsoft.com/2003/10/Serialization/"+">(.+?)</string>");
final Matcher matcher = pattern.matcher(str);
matcher.find();
System.out.println(matcher.group(1));
给定的String 格式是
<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">
तुम्हारा नाम क्या है
</string>
预期的输出是:
तुम्हारा नाम क्या है
它给了我一个错误
【问题讨论】:
-
一方面,正则表达式区分大小写。您的模式只会匹配
String [...]与大写的“S” -
请记住:您不能使用正则表达式解析 XML 或 HTML。理论见stackoverflow.com/questions/6751105/…,乐趣见stackoverflow.com/questions/1732348/… ...
-
补充 Jägermeister 的观点:stackoverflow.com/questions/701166/…