简体   繁体   中英

Regex for verifying content between tags in XML

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?

尝试这个

boolean mathes = str.matches(".*<(Expected|expected|Somethingexpected).*?>\\d+</\\1>.*");
<Somethingexpected xmlns="sw2223" xmlns=\"\">123</Somethingexpected> <Somethingexpected xmlns="sw2223" xmlns=\"\">123a</Somethingexpected> 

In cases like this, I expect the result should fail as there is 123a between one of the tags. But the test passes as it finds the first one to be valid as it has 123 between.. Hence I was wondering whether shoudn't I be using regex at all in this case or is there an alternative for this .. Thanks

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM