微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

XML文件中配置正则表达式

例XML:

<data type="REGEX">
<code>prop1</code>
<name>书名</name>
<method>
<![CDATA[
(?i)(?<=BookName\: \")([\u4e00-\u9fa5]+)
]]>
</method>
</data>

1)正则中含有<是会有错误的,需要进行处理;

2)注意和Java文件中的写法稍微不一样啊(反斜杠/) 多写一个结果就可能出不来了(⊙o⊙)哦


例Java:

public class TestRegex { public static void main(String[] args) throws Exception { String str ="BookName: \"我欲封天\",CategoryName: \"仙侠\",SubCategoryName: \"古典仙侠\""; String regex = "(?i)(?<=CategoryName\\: \")([\u4e00-\u9fa5]+)"; System.out.println(getRegexValue(regex,str,true).toString()); } public static Object getRegexValue(String regex,String html,boolean isList) { if (isList == false) { Pattern p = Pattern.compile(regex); Matcher matcher = p.matcher(html); return matcher.find() == true ? matcher.group() : null; } else { List<String> result = new ArrayList<String>(); Pattern p = Pattern.compile(regex); Matcher matcher = p.matcher(html); while (matcher.find()) { result.add(matcher.group()); } return result; } }}

原文地址:https://www.jb51.cc/xml/296172.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。