JAVA正则表达式的匹配机制

Posted on 2013-04-28 11:40  JasmineLiu  阅读(303)  评论(0编辑  收藏  举报

/**
*
* @author Jasmine
*/
public class TestPattern
{
public static void main(String[] args)
{
String patternString;
Pattern pattern ;
Matcher matcher;
String output;

patternString = "INTERVAL\\([0-9]\\)";
pattern = Pattern.compile(patternString,Pattern.CASE_INSENSITIVE);
matcher = pattern.matcher("INTERVAL(4)");
System.out.println(matcher.matches());

patternString = "INTERVAL DAY\\(\\d{1,9}\\) TO SECOND\\([0-9]\\)";
pattern = Pattern.compile(patternString,Pattern.CASE_INSENSITIVE);
matcher = pattern.matcher("INTERVAL DAY(123456789) TO SECOND(6)");
System.out.println(matcher.matches());
output = matcher.replaceAll("INTERVAL(6)");
System.out.println(output);

patternString = "INTERVAL YEAR\\(\\d{1,9}\\) TO MONTH";
pattern = Pattern.compile(patternString,Pattern.CASE_INSENSITIVE);
String s1 = "INTERVAL YEAR(4) TO MONTH";
matcher = pattern.matcher(s1);
System.out.println(matcher.matches());
output = matcher.replaceAll("INTERVAL");
System.out.println(output);
}
}

Copyright © 2024 JasmineLiu
Powered by .NET 9.0 on Kubernetes