出现次数:
次数 | 符号 |
---|
任意次 | * |
至少一次 | + |
最多一次 | ? |
确定n次 | {n} |
n到m次 | {n,m}(m缺省无穷大) |
内容:
内容 | 符号 |
---|
任意字符 | . |
字母数字下划线 | \w (\W非字符) |
数字 | \d (\D表示非数字) |
仅由xyz组成 | [xyz]* |
换行 | \n |
回车 | \r |
换页 | \f |
制表符 | \t |
垂直制表符 | \v |
不可见的字符(空格,换页,换行) | \s或[\n\r\f\t\v] (\S可见字符) |
其他:
意义 | 符号 |
---|
转义字符 | (正则表达式中已经有用的到字符{}.+-?* |
子表达式的 | () |
不包含 | [^] |
匹配开头 | ^ |
匹配结尾 | $ |
或 | |
常用正则表达式
意义 | 符号 |
---|
字母数字下划线,字母开头 | ^[a-zA-Z]\w{5,15}$ |
电话号xxx/xxxx-xxxxxxx/xxxxxxxx | ^(\d{3,4}-)\d{7,8}$ |
身份证号 | ^\d{15}(\d{2}[0-9xX])?$ |
正整数 | ^+?[1-9][0-9]*$ |
URL | ^(http |
日期 | \d{4}[年|\-|\.]\d{\1-\12}[\月|\-|\.]\d{\1-\31}日? |
首尾空白字符串 | ^\S*|\S*$ |
java正则表达式使用
匹配查询:
String string="asdfw^^ddsfsd"
Pattern pattern=Pattern.compile("[0-1,a-z,A-Z,+,/]+[=]*")
Matcher matcher=pattern.matcher(string)
while (matcher.find()) {
System.out.println(matcher.group())
}
替换:
String string="asdfw^^ddsfsd";
String s1=string.replaceAll("[0-1,a-z,A-Z,+,/]+[=]*", "");
匹配:
String string="asdfw^^ddsfsd";
String regExp="[0-1,a-z,A-Z,+,/]+[=]*";
Pattern pattern=Pattern.compile(regExp);
Matcher matcher=pattern.matcher(string);
boolean res=matcher.matches();
boolean res2=string.matches(regExp);