Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java regex extract data between a href tags

Tags:

java

regex

I am trying to extract data between a href tags in a Java string. I can acheive this with replace all and substring and with using indexOf etc.

I would like to know how can I get data using regex.

So basically i am trying to extract data and store in a string or in a list.

String data ="12345";
        String sampleStr ="";
        for(int i=0; i<10; i++) {
         data+=i;
        sampleStr += "<a href=\"javascript:yyy_getDetail(\'"+data+"\')\">"+data+"</a>"+", ";
        }           

        System.out.println(sampleStr);
        String temp = sampleStr.substring(sampleStr.indexOf("\">")+2);

Any suggestion in regard will be appreciated. What should be regex, so i only extract data.

like image 790
Nomad Avatar asked Mar 05 '13 14:03

Nomad


1 Answers

Here is an example for your needs. Note, that the full match will contain the string with anchor tags and your searched content is in the group 1.

String data ="12345";
String sampleStr ="";
for(int i=0; i<10; i++) 
{
 data+=i;
 sampleStr += "<a href=\"javascript:yyy_getDetail(\'"+data+"\')\">"+data+"</a>"+", ";
} 

Pattern pattern = Pattern.compile("<a[^>]*>(.*?)</a>");
Matcher matcher = pattern.matcher(sampleStr );
while (matcher.find()) 
{
        System.out.println("Result "+ matcher.group(1));
}
like image 190
VladL Avatar answered Sep 28 '22 04:09

VladL