1.验证手机号
匹配如下四个手机号
18079143050
15160819850
18765168050
14235412810
解析:
找规则:
1.以1开头 ==================== ^1
2.一共11位
3.50结尾===================== (50)$
4.倒数第三位是 0 或者 8========= [08]
合在一起为:1\d{7}[08](50)$
使用在线正则表达式工具检验一下:

编写代码:
public class Demo01 {
public static void main(String[] args) {
String telephoneNumber = "18079143050";
String telephoneNumber2 = "15160819850";
String telephoneNumber3 ="18765168050";
String telephoneNumber4 ="14235412810";
String regex = "^1\\d{7}[08](50)$";
System.out.println(telephoneNumber.matches(regex));
System.out.println(telephoneNumber2.matches(regex));
System.out.println(telephoneNumber3.matches(regex));
System.out.println(telephoneNumber4.matches(regex));
}
}
运行结果
true
true
true
false
Process finished with exit code 0
2 . 将is替换替换成大写的IS
He is a man
This is a boy
is that your car?
isn’t it?
What is your name?
解析:
题干要求找出单独的 is 单词,而不是this, isn’t这样的单词
在线正则工具:

编写代码:
public class Demo01 {
public static void main(String[] args) {
String s = "He is a man\n" +
"This is a boy\n" +
"is that your car?\n" +
"isn’t it?\n" +
"What is your name?";
String regex = "\\bis\\b";
System.out.println(s.replaceAll(regex, "IS"));
}
}
运行结果:
He IS a man
This IS a boy
IS that your car?
isn’t it?
What IS your name?
Process finished with exit code 0
3.日期替换
2016/03/05
1993/08/05
2016/11/24
12345/23/45678
12345/24/3356
1993-08-05
将正确的日期格式替换为03-05-2016
解析:
读题了解到需要查找的格式
1.先由4个数字组成年========== \d{4}
2.分割符号两种============== [-/]
3.两个数字组成的月份和日期==== \d{2}
4. 单词的分界处============== \b
组合成:\b(\d{4}[-/](\d{2})[-/](\d{2})\b
在线正则表达式工具:

编写代码:
public class Demo01 {
public static void main(String[] args) {
String r = "2016/03/05\n" +
"1993/08/05\n" +
"2016/11/24\n" +
"12345/23/45678\n" +
"12345/24/3356\n" +
"1993-08-05";
String regex = "\\b(\\d{4})[-/](\\d{2})[-/](\\d{2})\\b";
System.out.println(r.replaceAll(regex, "$2-$3-$1"));
}
}
运行结果:
03-05-2016
08-05-1993
11-24-2016
12345/23/45678
12345/24/3356
08-05-1993
Process finished with exit code 0
4. 获取以下英文句子中两个字符组成的单词并且输出
On Friendship And a youth said, “Speak to us of Friendship.”
Your friend is your needs answered. He is your field which
解析:
[A-Za-z]------表示所有的英文字母;
\b--------------单词的开头或结尾,也就是单词的分界处。
正则表达式为:
\b[A-Za-z][A-Za-z]\b
在线正则表达式工具检验:

编写代码:
public class Demo01 {
public static void main(String[] args) {
String W = "On Friendship And a youth said, " +
"\"Speak to us of Friendship.\" " +
"Your friend is your needs answered. He is your field which";
String regex = "\\b[A-Za-z][A-Za-z]\\b";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(W);
while (matcher.find()) {
System.out.print(" " + matcher.group());
}
}
}
运行结果:
On to us of is He is
Process finished with exit code 0
5.将Hello402World13Java798Android56PHP 字符串中的数字提取出来并且转化成一个排好序的字符串输出。
例如 0123456789
解析:
这题只需要找出数字即可
\d-----------数字
编写代码
public class Demo01 {
public static void main(String[] args) {
String n = "Hello402World13Java798Android56PHP";
String regex = "\\d+";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(n);
String result = "";
while (m.find()){
String group = m.group();
result += group;
}
char[] chars = result.toCharArray();
Arrays.sort(chars); //升序
System.out.println(chars);
}
}
运行结果:
0123456789
Process finished with exit code 0
总结:
这几道正则表达式的题比较基础,刚学习的同学可以试着做一下,可以很好的掌握和分析正则的使用方法和原则。
本文通过手机号验证、单词替换、日期格式修正、两字符单词提取和数字排序等实例,详细解析了正则表达式的基本用法和规则,帮助初学者快速上手正则表达式。

被折叠的 条评论
为什么被折叠?



