正则表达式
正则表达式:正确规则的表达式,是一个独立的语法,更多语言都支持正则表达式,他的作用就是用来的定义一些规则,去校验这个数据,是否符合我们所定义的规则。
日常使用时可网上查找.
定义正则
String regx = "a";
regx = "[a,b,c,d,A,Z,X]"; //出现我这个集合里面的任意一个字符,都算匹配上
regx = "[0,1,2]";
regx = "[a-z]";//所有小写字母
regx = "[A-Z]";//所有大写字母
regx = "[0-9]";//数字
regx = "[0-9a-zA-Z]";//所有字母和数字
regx = "[^A-Z]"; //^ 取反的意思
regx = "."; //匹配单个任意字符
regx = "\\."; //只匹配点本身 \\ 转义符
regx = "\\|"; // 转义 \\
regx = "\\d"; //跟这个意思[0-9]一样
regx = "\\w";// 跟这个意思[a - zA - Z_0 - 9]一样
regx = "ba?"; //? 一次 或一次也没有 ""就代表一次也没有
regx = "[a-z]*"; // * 0次或多次
regx = "[a-zA-Z0-9]+"; //+ 1次或多次
regx = "[a-z]{5,}"; // 至少5次
regx = "[0-9]{5}"; //正好5个
regx = "[0-9]{5,15}"; // 5<=个数<=15
案例演示
1,正则表达式
public static void main(String[] args) {
/* B:
案例演示
需求:校验qq号码.
1:要求必须是5 - 15 位数字
2:0 不能开头*/
Scanner scanner = new Scanner(System.in);
System.out.println("请输入你的qq号");
String qqnum = scanner.nextLine();
boolean flag =checkqqnum(qqnum);
if(flag){
System.out.println("符合规则");
}else {
System.out.println("不符合规则");
}
}
private static boolean checkqqnum(String qqnum) {
String regx = "[1-9][0-9]{4,14}";//第一位为1-9,后面的数为0-9,总共4-14位数.
boolean matches = qqnum.matches(regx);
return matches;
}
2,正则表达式替换的功能 public String replaceAll (String regex, String replacement)
String str="abc254454阿斯顿法定发放44dddd4444444eee111阿斯顿发射点发生111ffff阿斯顿发射点发大水5585ggggg";
//根据正则来替换掉
String s = str.replaceAll("[^0-9a-zA-Z]+", "");
System.out.println(s);
3,字符串:”91 27 46 38 50”,请写代码实现最终输出结果是:”27 38 46 50 91”
-
- 分析:
- a: 定义目标字符串"91 27 46 38 50"
- b: 对这个字符串进行切割,得到的就是一个字符串数组
- c: 把b中的字符串数组转换成int类型的数组
- (1): 定义一个int类型的数组,数组的长度就是字符串数组长度
- (2): 遍历字符串数组,获取每一个元素.将其转换成int类型的数据
- (3): 把int类型的数据添加到int类型的数组的指定位置
- d: 排序
- e: 创建一个StringBuilder对象,用来记录拼接的结果
- f: 遍历int类型的数组, 将其每一个元素添加到StringBuilder对象中
- g: 就是把StringBuilder转换成String
- h: 输出*/
String str = "91 27 46 38 50 9 550 63";
String[] s = str.split(" +");//去掉空格
int[] arr = new int[s.length];//创建与字符串长度相同的数组
for (int i = 0; i < s.length; i++) {
arr[i] = Integer.parseInt(s[i]);//用十进制数表示出来
}
Arrays.sort(arr);//排序
StringBuffer sb = new StringBuffer();//创建容器
for (int i = 0; i < arr.length; i++) {
sb.append(arr[i]).append(" ");//往容器中添加排好序的元素,后边加上空格。
}
String s1 = sb.toString().trim();//去除两端空格,将容器的类型转化为string
System.out.println(s1);
//打印结果:
// 9 27 38 46 50 63 91 550
4,需求:获取下面这个字符串中由三个字符组成的单词
da jia ting wo shuo, jin tian yao xia yu, bu shang wan zi xi, gao xing bu?
/*
思路:使用正则筛选符合条件的单词
*/
String str = "da jia ting wo shuo, jin tian yao xia yu, bu shang wan zi xi, gao xing bu?";
//去除除小写字母以外的字符
String s = str.replaceAll("[^a-z]"," ");
//去空格
String[] string = s.split(" +");
//装入容器
StringBuffer sb = new StringBuffer();
for (int i = 0; i < string.length; i++) {
if(string[i].length() == 3){//如果长度小于三,则装入容器
sb.append(string[i]).append(" ");
}
}
System.out.println(sb);
/*
结果:jia jin yao xia wan gao
*/
//patte与matcher方式查找
String regx="\\b[a-z]{3}\\b";
//patte模式器
Pattern p = Pattern.compile(regx);
//Matcher查找器
Matcher matcher = p.matcher(str);
//循环查找,
// find()查找
// group()匹配获取
while (matcher.find()){
String group = matcher.group();
System.out.println(group);
}
5,Pattern和Matcher的概述
public static void main(String[] args) {
//Pattern 模式器: 把正则表达式封装进该类
// Matcher 匹配器
//1.把正则表达式封装到模式器中
Pattern p = Pattern.compile("a*b");//条件
//2.通过模式器中的matcher方法,又获取到了一个匹配,并把我们要匹配的数据传进去
Matcher m = p.matcher("aaaaab");
//3.调用匹配器中的matches()方法,看这个数据符不符合正则
boolean b = m.matches();//ture
System.out.println(b);
boolean b1 = "aaaaab".matches("a*b");
System.out.println(b1);
6,用模式去与匹配器实现简易爬虫
String regx = "(http|https):\\/\\/([\\w.]+\\/?)\\S*";
Pattern compile = Pattern.compile(regx);
Matcher matcher = compile.matcher(str);
//boolean b = matcher.find();
// System.out.println(b);
while (matcher.find()) {
String group = matcher.group();
System.out.println(group);