java中使用正则表达式
常用匹配
public static void main(String [] args) {
String content = "hello,I am snackfish,nice to meet you !";
String pattern = "hello,I am (.*)(you) !";
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(content);
System.out.println(m.matches());
m.reset();
if(m.matches()) {
System.out.println("matches 结果 " + m.group(1));
}
}
matches、find、lookingAt方法的区别
public static void main(String [] args) {
String content = "hello,I am snackfish,nice to meet you !";
String pattern = "hello,I am (.*)(you) !";
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(content);
System.out.println(m.matches());
m.reset();
if(m.matches()) System.out.println("matches 结果 " + m.group(1));
m.reset();
System.out.println(m.find());
m.reset();
if(m.find()) System.out.println("find 结果 " + m.group(1));
m.reset();
System.out.println(m.lookingAt());
m.reset();
if(m.lookingAt())System.out.println("lookingAt 结果" + m.group(1));
}
JS中使用正则表达式
var str = 'hello,I am snackfish,nice to meet you !';
var pattern = /hello,I am (.*)(you) !/;
var arrs = str.match(pattern);