I am trying to use regex for verifying contents in an XML file. I have tried the following things.
XML file 1:
<start>
<hi>2dsds</hi>
<expected xmlns="sw2223" xmlns=\"\">123</expected>
<bye>2dsds</bye>
XML file 2:
<start>
<hi>2dsds</hi>
<Somethingexpected xmlns="sw2223" xmlns=\"\">123</Somethingexpected>
<bye>2dsds</bye>
In these two XML files, I am concerned about the contents between the fields <expected>
and <Somethingexpected>
. I want each and every field between that content to be numeric.
Valid contents:
<Somethingexpected xmlns="sw2223" xmlns=\"\">123</Somethingexpected>
<Expected xmlns=\"\">123</Expected>
<expected xmlns=\"\">123</expected>
Invalid contents:
<Somethingexpected xmlns="sw2223" xmlns=\"\">123a</Somethingexpected>
<Expected xmlns=\"\">avbv 123</Expected>
<expected xmlns=\"\">**(***</expected>
I don't need anything other that a number between the tags (not even a space)
I have tried using these regular expressions:
if(String.matches(".*<.*[eE]xpected.*?>.*[a-zA-Z].*<.*") ||
String.matches(".*<.*[eE]xpected.*?>.*[^0-9].*<.*"))
return invalid;
else
return valid;
Input 1:
<Somethingexpected xmlns="sw2223" xmlns=\"\">123</Somethingexpected>
Input 2:
<start>
<hi>2dsds</hi>
<Somethingexpected xmlns="sw2223" xmlns=\"\">123</Somethingexpected>
<bye>2dsds</bye>
For input 1, this says valid. For input 2 it says invalid
I am not sure where I am going wrong. Could anyone correct my regexes?