我正尝试使用正则表达式来验证XML文件中的内容。我尝试了以下事情。用于验证XML中标签之间内容的正则表达式
XML文件1:
<start>
<hi>2dsds</hi>
<expected xmlns="sw2223" xmlns=\"\">123</expected>
<bye>2dsds</bye>
XML文件2:
<start>
<hi>2dsds</hi>
<Somethingexpected xmlns="sw2223" xmlns=\"\">123</Somethingexpected>
<bye>2dsds</bye>
在这两个XML文件,我关注的领域<expected>
和<Somethingexpected>
之间的内容。我希望内容之间的每个领域都是数字。
有效内容:
<Somethingexpected xmlns="sw2223" xmlns=\"\">123</Somethingexpected>
<Expected xmlns=\"\">123</Expected>
<expected xmlns=\"\">123</expected>
无效的内容:
<Somethingexpected xmlns="sw2223" xmlns=\"\">123a</Somethingexpected>
<Expected xmlns=\"\">avbv 123</Expected>
<expected xmlns=\"\">**(***</expected>
我不需要其他任何标签之间的数字(甚至没有空格)
我曾尝试使用这些正则表达式:
if(String.matches(".*<.*[eE]xpected.*?>.*[a-zA-Z].*<.*") ||
String.matches(".*<.*[eE]xpected.*?>.*[^0-9].*<.*"))
return invalid;
else
return valid;
输入1:
<Somethingexpected xmlns="sw2223" xmlns=\"\">123</Somethingexpected>
输入2:
<start>
<hi>2dsds</hi>
<Somethingexpected xmlns="sw2223" xmlns=\"\">123</Somethingexpected>
<bye>2dsds</bye>
对于输入1,则该说有效。对于输入2,它说无效
我不知道我哪里出错了。任何人都可以纠正我的正则表达式吗?
首先,你对原始XML使用正则表达式。使用XPath首先提取值。 –