0
我目前使用dom4j從XML讀取正則表達式模式,但匹配函數總是返回false,即使通過字符串返回是完全一樣的,我已經在XML中鍵入。DOM4J閱讀正則表達式BUG
樣本XML:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<username-pattern><![CDATA[^\\w+$]]></username-pattern>
</root>
示例代碼(不工作):
public class Test
{
public static void main(String[] args) throws DocumentException
{
File inputFile = new File("C://test/test.xml");
SAXReader saxBuilder = new SAXReader();
Document document = saxBuilder.read(inputFile);
Element rootElement = document.getRootElement();
String username_pattern_notwork = rootElement.elementTextTrim("username-pattern");
String username_pattern_work = "^\\w+$";
if(!(Pattern.matches(username_pattern_notwork, "ooo"))){
System.out.println("===============Err=========");
}else{
System.out.println("OK");
}
}
}
如果更換可變username_pattern_notwork與username_pattern_work在Pattern.matches(username_pattern_notwork, 「嗚嗚嗚」),它將正常工作
幾乎是正確的,不完全是。從外部源讀取的字符串不需要轉義:只有Java字符串文字需要轉義。 –