Skip to content

Instantly share code, notes, and snippets.

@damog
Created March 16, 2009 20:55
Show Gist options
  • Save damog/80064 to your computer and use it in GitHub Desktop.
Save damog/80064 to your computer and use it in GitHub Desktop.
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class RegexCaptureTest {
// public static void main(String[] args){
public static void main(String[] args){
String inputStr = "<a href=\"http://google.com/\">Visit Google today!</a>";
String patternStr = "<a href=\"(.+?)\">(.+?)</a>";
Pattern pattern = Pattern.compile(patternStr);
Matcher matcher = pattern.matcher(inputStr);
if(matcher.find()) {
for (int i = 0; i <= matcher.groupCount(); i++) {
System.out.printf("coincidence %d: ", i);
System.out.println(matcher.group(i));
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment