正则表达式
正则表达式,又称规则表达式,(Regular Experssion,在代码中常简写为regex、regexp或RE),是一种文本模式,包括普通字符(例如,a到z之间的字母)和特殊字符(称为“元字符”)。正则表达式使用单个字符串来描述、匹配一系列匹配某个句法规则的字符串,通常被用来检索、替换那些符合某个模式(规则)的文本。
String支持正则表达式的方法
(1)String.replaceAll()
(2)String.replaceFirst()
(3)String.split()
(4)String.matches()
支持正则表达式的类
(1)java.util.regex .Pattern
Pattern 类:pattern 对象是一个正则表达式的编译表示。Pattern 类没有公共构造方法。要创建一 个 Pattern 对象,你必须首先调用其公共静态编译方法,它返回一个 Pattern 对象。该方法接受一个正 则表达式作为它的第一个参数。
正则表达式基本语法规则
正则表达式的使用
1. 判断字符串有没有大写字母。
2. 判断字符串有没有汉字。
3. 判断字符串有没有数字。
4. 判断一个字符串是不是标准的手机号。
public class RegEx2 {
public static void main(String[] args) {
//1.判断字符串是不是由大写字母组成
System.out.println("Hello".matches("[A-Z]+"));
//判断字符串中有没有大写字母
System.out.println("Hello".matches(".*[A-Z]+.*"));
System.out.println("hello".matches(".*[A-Z]+.*"));
//判断字符串中有没有汉字
System.out.println("hello中文".matches(".*[\u4e00-\u9fa5].*"));
//判断字符串中有没有数字
//"\\d"=="[0-9]"表示数字
System.out.println("hellojava中文".matches(".*\\d.*"));
System.out.println("hellojava17中文".matches(".*[0-9].*"));
//判断字符串中有没有手机号
System.out.println("hellojava1783735121中文".matches(".*1[3578]\\d{9}.*"));
//ctrl+shift+F 项目查找,支持正则表达式
//ctrl+shift+R 项目查找,支持正则表达式
}
}
运行结果:
5、注册项目时,用户的姓名要么是全中文,要么是全英文,不能其它符号,不可以使用混合。
6、判断输入的手机号是否正确。
public class RegEx1 {
public static void main(String[] args) {
//判断字符串是不是中文字符?
Scanner sc = new Scanner(System.in);
System.out.print("请输入姓名:");
String name = sc.nextLine();
if (name.matches("[\u4e00-\u9fa5]+")) {
//汉字正则表达式范围:[\u4e00-\u9fa5]
System.out.println("欢迎:" + name);
} else {
System.out.println("welcome:" + name);
}
/* if (name.matches(".*[\u4e00-\u9fa5].*")) {
System.out.println("欢迎:" + name);
} else {
System.out.println("welcome:" + name);
}*/
System.out.println("");
//判断一个手机号是不是正确的手机号?
System.out.print("请输入你的手机号:");
String phone = sc.nextLine().trim();
//1[358]\\d{9}:1开头,[358]第二位必须是358其中一位,\\d{9}表示后面九位全是数字
if (phone.matches("1[358]\\d{9}")) {
System.out.println("手机号正确:" + phone);
} else {
System.out.println("错误的手机号");
}
/* boolean f = true;
if (phone.length() == 11 && phone.startsWith("1")) {
for (int i = 1; i < phone.length(); i++) {
char c = phone.charAt(i);
if (c >= '0' && c <= '9') {
} else {
f = false;
break;
}
}
} else {
f = false;
}
if (f) {
System.out.println("手机号正确:" + phone);
} else {
System.out.println("错误的手机号");
}*/
}
}
运行结果:
7、求123456789 字符串1..9 之间不改数字位置随机+-要求表达结果和为100,写出这样表达式 。
public class RegEx5 {
public static void main(String[] args) {
Set set=new HashSet();
while (set.size()!=11) {
//++count;
String s = getString();
if (getSum(s) == 100) {
set.add(s);
}
}
System.out.println(set.toString());
}
public static String getString() {
StringBuilder su = new StringBuilder("1");
String[] ps = {"", "+", "-"};
Random rand = new Random();
for (int i = 2; i <=9; i++) {
su.append(String.format("%s%d", ps[rand.nextInt(3)], i));
}
return su.toString();
}
public static int getSum(String s) {
Pattern p = Pattern.compile("-?\\d+");//正则表达式
Matcher matcher = p.matcher(s);//匹配器
int sum = 0;
while (matcher.find()) {//根据表达式找,找到就为true
int n = Integer.parseInt(matcher.group());//将找到的结果转成数字
sum += n;
}
return sum;
}
}
运行结果: