Question: I'll be the first to admit that my Regex knowledge is hopeless. I am using java with the following

```
Matcher m = Pattern.compile(">[^<>]*</a>").matcher(html);
while (m.find()) {
 resp.getWriter().println(html.substring(m.start(), m.end()));
}

```

I get the following list:

```
>Link Text a</a>
>Link Text b</a>

```

What am I missing to remove the `>` and the `</a>`.

Cheers.

Answer: 
You can do that by wrapping a group around that part of your regex and then using `group(X)` where `X` is the number of the group:

```
Matcher m = Pattern.compile(">([^<>]*)</a>").matcher(html);
while (m.find()) {
 resp.getWriter().println(m.group(1));
}

```

But, a better way would be to use a simple parser for this:

```
import java.io.*;
import javax.swing.text.*;
import javax.swing.text.html.*;
import javax.swing.text.html.parser.*;

public class HtmlParseDemo {
   public static void main(String [] args) throws Exception {
       Reader reader = new StringReader("foo <a href=\"#\">Link 1</a> bar <a href=\"#\">Link <b>2</b> more</a> baz");
       HTMLEditorKit.Parser parser = new ParserDelegator();
       parser.parse(reader, new LinkParser(), true);
       reader.close();
   }
}

class LinkParser extends HTMLEditorKit.ParserCallback {

    private boolean linkStarted = false;
    private StringBuilder b = new StringBuilder();

    public void handleText(char[] data, int pos) {
        if(linkStarted) b.append(new String(data));
    }

    public void handleStartTag(HTML.Tag t, MutableAttributeSet a, int pos) {
        if(t == HTML.Tag.A) linkStarted = true;
    }

    public void handleEndTag(HTML.Tag t, int pos) {
        if(t == HTML.Tag.A) {
            linkStarted = false;
            System.out.println(b);
            b = new StringBuilder();
        }
    }
}

```

Output:

```
Link 1
Link 2 more

```
Have you looked at using a [capturing group](http://www.exampledepot.com/egs/java.util.regex/Group.html) ?

```
Pattern.compile(">([^<>]*)</a>")

```

Note however that it's generally *not recommended* to use regular expressions for HTML, since HTML isn't regular. You will get more reliable results by using an HTML parser such as [JTidy](http://jtidy.sourceforge.net/).