Created
December 13, 2011 17:30
-
-
Save enmanuelr/1473036 to your computer and use it in GitHub Desktop.
java group matching sucks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.regex.Pattern; | |
import java.util.regex.Matcher; | |
public class Test { | |
public static void main(String[] args) { | |
if (args.length < 2) { | |
System.out.println("Usage: [command] [string] [regex]"); | |
return; | |
} | |
Pattern pattern = Pattern.compile(args[1]); | |
Matcher matcher = pattern.matcher(args[0]); | |
System.out.println(matcher.matches()); | |
if(matcher.find()) | |
System.out.println("Found " + matcher.groupCount() + " match(es)"); | |
else | |
return; | |
for(int cont = 0; cont < matcher.groupCount(); cont++) | |
System.out.println("Group " + (cont + 1) + ": " + matcher.group(cont + 1)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment