例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.f2er.com/xml/296172.html