一、编程语法
java语言
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Test {
public static void main(String args[]) {
String str = "test123";
String pattern = "test(.*)";
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(str);
if (m.find()){
// 输出123
System.out.println(m.group(1));
}
// 输出true
System.out.println(m.matches());
}
}
python语言
import re
pattern = re.compile(r'test(.*)')
str = 'test123'
print(pattern.findall(str)) # 输出['123']
print(pattern.match(str)) # 输出<re.Match object; span=(0, 7), match='test123'>
print(pattern.search(str)) # 输出<re.Match object; span=(0, 7), match='test123'>
二、常用正则语法
说明:图片截取于r2coding
参考:菜鸟正则教程