前端之家收集整理的这篇文章主要介绍了
正则表达式_Java,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
正则表达式
正则表达式概述
@H_
502_28@
正则表达式:是指一个用来描述或者匹配一系列符合某个句法规则的字符串的单个字符串。是一个规则。
@H_
502_28@所以在Java中,把这个
规则直接定义为一个String:
String regex ="[1-9][0-9]{4,14}"
正则表达式规则(常用)
A:字符
x 字符 x。举例:'a'表示字符a
\\ 反斜线字符。
\n 新行(换行)符 ('\u000A')
\r 回车符 ('\u000D')
B:字符类
[abc] a、b 或 c(简单类)
[^abc] 任何字符,除了 a、b 或 c(否定)
[a-zA-Z] a到 z 或 A到 Z,两头的字母包括在内(范围)
[0-9] 0到9的字符都包括
C:预定义字符类
. 任何字符。我的就是.字符本身,怎么表示呢? \. (实际应用中,写为\\.)
\d 数字:[0-9] (实际应用中,写为\\d)
\w 单词字符:[a-zA-Z_0-9] (实际应用中,写为\\w)
在正则表达式里面组成单词的东西必须有这些东西组成
D:边界匹配器
^ 行的开头
$ 行的结尾 \b 单词边界 就是不是单词字符的地方。 举例:hello world?haha;xixi E:Greedy 数量词 X? 表示:X,一次或一次也没有 X* 表示:X,零次或多次 X+ 表示:X,一次或多次 X{n} 表示:X,恰好 n 次 X{n,} 表示:X,至少 n 次 X{n,m} 表示:X,至少 n 次,但是不超过 m 次
1. 判断功能
@H_502_28@String类的public boolean matches(String regex)
@H_
502_28@返回的是 boolean
@H_
502_28@eg1:(检验QQ号码)
public class RegexDemo2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入你的QQ号码:");
String qq = sc.nextLine();
System.out.println("checkQQ:" + checkQQ(qq));
}
public static boolean checkQQ(String qq) {
return qq.matches("[1-9]\\d{4,14}");
}
}
@H_
502_28@eg2:检验邮箱
public class RegexTest {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入邮箱:");
String email = sc.nextLine();
String regex = "\\w+@\\w{2,6}(\\.\\w{2,3})+";
boolean flag = email.matches(regex);
System.out.println("flag:"+flag);
}
}
2. 分割功能
@H_502_28@String类的public String[] split(String regex)
@H_
502_28@返回的是以个String数组
public class RegexDemo2 {
public static void main(String[] args) {
String s1 = "aa,bb,cc";
String[] str1Array = s1.split(",");
for (int x = 0; x < str1Array.length; x++) {
System.out.println(str1Array[x]);
}
System.out.println("---------------------");
String s2 = "aa.bb.cc";
String[] str2Array = s2.split("\\.");
for (int x = 0; x < str2Array.length; x++) {
System.out.println(str2Array[x]);
}
System.out.println("---------------------");
String s3 = "aa bb cc";
String[] str3Array = s3.split(" +");
for (int x = 0; x < str3Array.length; x++) {
System.out.println(str3Array[x]);
}
System.out.println("---------------------");
String s4 = "E:\\JavaSE\\day14\\avi";
String[] str4Array = s4.split("\\\\");
for (int x = 0; x < str4Array.length; x++) {
System.out.println(str4Array[x]);
}
System.out.println("---------------------");
}
}
3. 替换功能
@H_502_28@String类的public String replaceAll(String regex,String replacement)
@H_
502_28@使用给定的
replacement替换此字符串所有匹配给定的
regex。
public class RegexDemo {
public static void main(String[] args) {
String s = "helloqq12345worldkh622112345678java";
String regex = "\\d+";
String ss = "*";
String result = s.replaceAll(regex,ss);
System.out.println(result);
}
}
@H_
502_28@使用
方法:
Pattern和Matcher
Pattern p = Pattern.compile("a*b");
Matcher m = p.matcher("aaaaab");
find():查找存不存在
group():获取刚才查找过的数据
public class RegexDemo2 {
public static void main(String[] args) {
String s = "da jia ting wo shuo,jin tian yao xia yu,bu shang wan zi xi,gao xing bu?";
String regex = "\\b\\w{3}\\b";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(s);
while (m.find()) {
System.out.println(m.group());
}
}
}