正则语法
\d
\d 是一个正字0到9的数字
\d+ 表示+号表示至少要匹配一次 当在java字符串中要"^\\d+$"
\d* 表示0次或多次数字
如下例子
[Rr](\\d*) 表示 R或r 加上 0次或多次数字
R或r 加上 0次数字
R //R 加上 0次数字
r //r 加上 0次数字
R或r 加上 多次数字
R12 //R 加上 多次数字
r12 //r 加上 多次数字
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Cz {
private static final Pattern pattern = Pattern.compile("[Rr](\\d*)");
public static void main(String[] args) {
String[] weightArray = {"r12", "R12","r","R"};
for (String temp:weightArray) {
Matcher m = pattern.matcher(temp);
if (m.find()) {
System.out.println("获取的值为="+m.group(1));
} else{
System.out.println("没有找到匹配");
}
}
}
}
获取的值为=12 //当temp=r12时
获取的值为=12 //当temp=R12时
获取的值为= //当temp=r时
获取的值为= //当temp=R时
*
* 表示0次或多次匹配 zo* 表示
z //o 出现0次
zo //o 出现1次
zoo //o 出现2次
**:匹配路径中的零个或多个目录
com/**/config.xml
com/config.xml 匹配0个路径
com/feng/spring/config.xml 匹配一或两个路径
点.
点可以用来表示任意字符 .*?
\. 表示一个原生的点 .
[^abc]
[^abc] 任何字符,除了a、b 或 c(否定)
"[^abc]“匹配"plain"中"p”,“l”,“i”,“n”。
不能以xxx为开头的,以xxx为结尾之前的内容
[Rr]
表示 R或者r 再加上 详细请看 如下未例
?
- ? 要么不出现要么出现一次
- *? 重复任意次,但尽可能少重复
用两个\再加一个? 来表示问号的本身 - 点是任意字符
- *? 重复任意次,但尽可能少重复
- (.?)设置分组 (?.*?)
(?<start>.*?) 然后通过 matcher.group("start")来取
eg---------------------------
Matcher matcher = Pattern.compile(".*<StatusCode>(?<start>.*?)</StatusCode>.*")
.matcher("<core><StatusCode>0000</StatusCode></core>");
if(matcher.matches()){
System.out.println("group="+matcher.group());
System.out.println("matcher.group(1)="+matcher.group(1));
System.out.println("matcher.group(name)="+matcher.group("start"));
System.out.println("groupCount="+matcher.groupCount());
}
//最后打印结果为
group=<core><StatusCode>0000</StatusCode></core>
matcher.group(1)=0000
matcher.group(name)=0000
不能以?开头的一个或多个字符 换句话说 如果有?那么就是以?为结尾之前的一个或多个字符
Pattern patter = Pattern.compile("(\\d*\\.\\d*\\.\\d*\\.\\d*)\\:(\\d*)/(([^\\?]*))");
Matcher match = patter.matcher("jdbc:mysql://127.0.0.1:3306/maqingbin?useUnicode=true&characterEncoding=utf-8");
if(match.find()){
System.out.println(match.group(1)); //127.0.0.1
System.out.println(match.group(2)); //3306
System.out.println(match.group(3)); //maqingbin
}
正则的java语法
Matcher matcher=Pattern.compile("xxx具体的一个正则xxx").matcher("xx目标字符串xxx");
if(matcher.matches()){//匹配上了
matcher.group(1);//按顺序来获取值
matcher.group("xxnamexx");//按名字来获取值
}
while(matcher.find()){//如果多个都匹配上了
matcher.group("xxnamexx");//按名字来获取值
}
matches()与find()区别
Pattern intPattern = Pattern.compile("[0-9]+"); //判断是否为数字
Matcher m = intPattern.matcher("aaa010222");
System.out.print(m.matches()); //返回false因为matches()是整串匹配
System.out.print(m.find()); //返回true因为会找部分匹配
关于Matcher.find()方法说明
可以指定m.find(int start);指定从第几个索引处开始检索、匹配,在这个操作之前进行了reset操作
使用find()或find(int start) 如果匹配到了可以获取以下信息:
matcher.start() //匹配到的子字符串索引开始位置
matcher.end() //匹配到的子字符串索引结束位置
matcher.group() //返回匹配到的子字符串
手机号的判断
@Test
public void PatternTest() {
String mobile = "15011xxx22";
String pattern = "^(?=\\d{11}$)^1(?:3\\d|4[57]|5[^4\\D]|66|7[^249\\D]|8\\d|9[89])\\d{8}$";
boolean isMatch = Pattern.matches(pattern, mobile);
System.out.println("这个手机号为"+isMatch);
}
常用正则手机号判断
String pattern = "^(?=\\d{11}$)^1(?:3\\d|4[57]|5[^4\\D]|66|7[^249\\D]|8\\d|9[89])\\d{8}$";