我有一个带有一些自定义标记的文件,我想编写一个正则表达式来提取标记之间的字符串。例如,如果我的标签是:
[customtag]String I want to extract[/customtag]如何编写一个正则表达式来仅提取标记之间的字符串。这段代码似乎是朝着正确的方向迈出的一步:
Pattern p = Pattern.compile("[customtag](.+?)[/customtag]");
Matcher m = p.matcher("[customtag]String I want to extract[/customtag]");不知道下一步该做什么。有什么想法吗?谢谢。
发布于 2015-10-24 04:12:04
试试这个:
Pattern p = Pattern.compile(?<=\\<(any_tag)\\>)(\\s*.*\\s*)(?=\\<\\/(any_tag)\\>);
Matcher m = p.matcher(anyString);例如:
String str = "<TR> <TD>1Q Ene</TD> <TD>3.08%</TD> </TR>";
Pattern p = Pattern.compile("(?<=\\<TD\\>)(\\s*.*\\s*)(?=\\<\\/TD\\>)");
Matcher m = p.matcher(str);
while(m.find()){
Log.e("Regex"," Regex result: " + m.group())
}输出:
10烯
3.08%
https://stackoverflow.com/questions/6560672
复制相似问题