Every time I want to use a regular expression in a Java program, I always seem to go through a lot of iterations developing an expression to get the specific results I need. As a result, this is a small Java program that allows me to use command line arguments to specify text and expressions on the command line.
import java.util.*;
import java.util.regex.*;
public class Regex
{
public static void main (String [] args) throws Exception
{
String source = args[0];
System.out.println("Source string: " + source);
Pattern p = Pattern.compile(args[1]);
System.out.println("Pattern: " + args[1]);
Matcher m = p.matcher(source);
System.out.println("match? " + m.matches());
for (int i=1; i <= m.groupCount(); ++i)
{
System.out.println("\tGroup " + i + ":\t" + m.group(i));
}
}
}