java正则表达式
例子1
public void testa(){
Pattern p = Pattern.compile(“.*from.*”);
String x = “select cola from ta”;
Matcher m = p.matcher(x);
System.out.println(m.matches());//输出true
}
public void testb(){
Pattern p = Pattern.compile(“.*from.*”,Pattern.CASE_INSENSITIVE);
String x = “select cola FROM ta”; //FROM 大写
Matcher m = p.matcher(x);
System.out.println(m.matches());//输出false
}
public void testC(){
Pattern p = Pattern.compile(“.*from.*”,Pattern.CASE_INSENSITIVE);//忽略大小写
String x = “select cola FROM ta”;
Matcher m = p.matcher(x);
System.out.println(m.matches());//输出true
}
例子2
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexTestPatternMatcher {
public static final String EXAMPLE_TEST = “This is my small example string which I’m going to use for pattern matching.”;
public static void main(String[] args) {
Pattern pattern = Pattern.compile(“\w+”);
// In case you would like to ignore case sensitivity you could use this
// statement
// Pattern pattern = Pattern.compile(“\s+”, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(EXAMPLE_TEST);
// Check all occurance
while (matcher.find()) {
System.out.print(“Start index: ” + matcher.start());
System.out.print(” End index: ” + matcher.end() + ” “);
System.out.println(matcher.group());
}
// Now create a new pattern and matcher to replace whitespace with tabs
Pattern replace = Pattern.compile(“\s+”);
Matcher matcher2 = replace.matcher(EXAMPLE_TEST);
System.out.println(matcher2.replaceAll(“t”));
}
}
参考资料:
http://www.vogella.de/articles/JavaRegularExpressions/article.html
近期评论