raka
raka

Reputation: 365

Java's pattern matcher says no match found but matches are print to the console

I've written a matcher to match from the following text.

<TD><P>xyz...  </P><P>Oiuye </P><P>Oture, </P><P>Option</P></TD><TD><P> EUR 0.20 </P></TD><Span></Span>     
<TD><P>Foriegn </P></TD><TD><P> EUR 0.30 </P></TD><Span></Span>

The Pattern I want to use is:-

Pattern p = Pattern.compile("\\</TD\\>\\<TD\\>\\<P\\>(.*?)\\</P\\>");Matcher m_Fee_1 = p_Fee_1.matcher(row_xml);          
m_Fee_1.find();
String Contract_Fee_Temp = m_Fee_1.group(1).trim(); 

I need to capture EUR 0.20 and EUR 0.30.

My console shows EUR 0.20 EUR 0.30

And throws an Error, No match found. Why does this happen? Is it okay if I just catch that exception and take the data? Or how should I handle it?

Upvotes: 1

Views: 233

Answers (2)

Rakesh Reddy
Rakesh Reddy

Reputation: 136

You just can catch the exception if you are already getting what you require.

try{
Pattern p = Pattern.compile("\\</TD\\>\\<TD\\>\\<P\\>(.*?)\\</P\\>");Matcher m_Fee_1 = p_Fee_1.matcher(row_xml);          
m_Fee_1.find();
String Contract_Fee_Temp = m_Fee_1.group(1).trim(); 
//Print what ever you want
}
catch(IllegalStateException exception){}

Upvotes: 1

npinti
npinti

Reputation: 52185

I suppose you are using the matcher incorrectly:

    String[] str = new String[]{"<TD><P>xyz...  </P><P>Oiuye </P><P>Oture, </P><P>Option</P></TD><TD><P> EUR 0.20 </P></TD><Span></Span> ", "<TD><P>Foriegn </P></TD><TD><P> EUR 0.30 </P></TD><Span></Span>"};
    Pattern p = Pattern.compile("\\</TD\\>\\<TD\\>\\<P\\>(.*?)\\</P\\>");
    for (String st : str) {
        Matcher m = p.matcher(st);
        while (m.find()) {
            System.out.println(m.group(1));
        }
    }

Yields:

EUR 0.20 
EUR 0.30

Upvotes: 3

Related Questions