[阅读: 322] 2006-08-10 09:26:39
import org.apache.oro.text.regex.MalformedPatternException;
import org.apache.oro.text.regex.MatchResult;
import org.apache.oro.text.regex.Pattern;
import org.apache.oro.text.regex.PatternCompiler;
import org.apache.oro.text.regex.PatternMatcher;
import org.apache.oro.text.regex.Perl5Compiler;
import org.apache.oro.text.regex.Perl5Matcher;
public class Perl5RegExprHelper {
public static MatchResult ( String pattern, String str )
{
try{
PatternCompiler pc = new Perl5Compiler();
PatternMatcher pm = new Perl5Matcher();
Pattern pattern = pc.compile(pattern);
pm.contains(str, pattern);
return pm.getMatch();
}catch(Exception e){
return null;
}
}
public class Test{
public static void main(String[] args)
{
MatchResult r = Perl5RegExprHelper("([0-9]{1,3})\\.([0-9]{1,3})\\.([0-9]{1,3})\\.([0-9]{1,3})","192.168.0.1");
System.out.println(mr.group(1)+" "+mr.group(2)+" "+mr.group(3)+" "+mr.group(4));
}
}