1、概念:
用于验证字符串是否符合格式的字符串。
2、代码示例
public class Demo9_正则表达式 {
public static boolean isOk(String qq) {
// //首数字不能为0
// if (qq.charAt(0) == '0') {
// return false;
// }
// //长度为6~11位
// if (qq.length() < 6 || qq.length() > 11) {
// return false;
// }
// //必须全数字
// try {
// Integer i = new Integer(qq);
// return true;
// } catch (NumberFormatException e) {
// return false;
// }
String regex = "[1-9]\\d{6,11}";
return qq.matches(regex);
//问题
//1、数据验证要求越高,判断越多,越复杂。
//2、导致主营业务编程无法进行,收到影响。
//解决:使用正则表达式
}
public static void main(String[] args) {
//需求验证QQ号码是否正确
String qq = "574327563";
boolean ok = isOk(qq);
System.out.println(ok);
}
}
3、正则表达式的组成
(1)Characters(字符)
x The character x
\\ The backslash character
\0n The character with octal value 0n (0 <= n <= 7)
\0nn The character with octal value 0nn (0 <= n <= 7)
\0mnn The character with octal value 0mnn (0 <= m <= 3, 0 <= n <= 7)
\xhh The character with hexadecimal value 0xhh
\uhhhh The character with hexadecimal value 0xhhhh
\x{h...h} The character with hexadecimal value 0xh...h (Character.MIN_CODE_POINT <= 0xh...h <= Character.MAX_CODE_POINT)
\t The tab character ('\u0009')
\n The newline (line feed) character ('\u000A')
\r The carriage-return character ('\u000D')
\f The form-feed character ('\u000C')
\a The alert (bell) character ('\u0007')
\e The escape character ('\u001B')
\cx The control character corresponding to x
public class Demo9_正则表达式 {
public static void main(String[] args) {
//需求:验证手机号码第一个数字
String regex = "1";//验证的字符串第一个位必须是1且仅有一个1
String mobile = "11";
boolean matches = mobile.matches(regex);
System.out.println(matches);//true
//method_示例();
}
}
2)、Character classes(字符类)
[abc] a, b, or c (simple class)
[^abc] Any character except a, b, or c (negation)
[a-zA-Z] a through z or A through Z, inclusive (range)
[a-d[m-p]] a through d, or m through p: [a-dm-p] (union)
[a-z&&[def]] d, e, or f (intersection)
[a-z&&[^bc]] a through z, except for b and c: [ad-z] (subtraction)
[a-z&&[^m-p]] a through z, and not m through p: [a-lq-z](subtraction)
public class Demo9_正则表达式 {
public static void main(String[] args) {
//需求:验证手机号码第1,2位数字
String regex = "1[35789]";
String mobile = "14";
boolean matches = mobile.matches(regex);
System.out.println(matches);
}
}
(3)Greedy quantifiers(数量词)
X? X, once or not at all(x出现1次或0次)
X* X, zero or more times(x出现0次或多次)
X+ X, one or more times(x出现1次或多次)
X{n} X, exactly n times(x出现n次)
X{n,} X, at least n times(x出现最少n次)
X{n,m} X, at least n but not more than m times(x出现最少n次,不超过m次)
public class Demo9_正则表达式 {
public static void main(String[] args) {
//需求:验证手机号码
String regex = "1[35789][0-9]{9}";
String mobile = "13712341234";
boolean matches = mobile.matches(regex);
System.out.println(matches);
}
}
(4)Predefined character classes(预定义字符类)
. Any character (may or may not match line terminators)
\d A digit: [0-9](数字)
\D A non-digit: [^0-9](非数字)
\s A whitespace character: [ \t\n\x0B\f\r](空白字符)
\S A non-whitespace character: [^\s](非空白字符)
\w A word character: [a-zA-Z_0-9](单词字符)
\W A non-word character: [^\w](非单词字符)
public class Demo9_正则表达式 {
public static void main(String[] args) {
//需求:验证手机号码
String regex = "1[35789]\\d{9}";
String mobile = "13712341234";
boolean matches = mobile.matches(regex);
System.out.println(matches);
}
}
(5)Boundary matchers(边界匹配器)
^ The beginning of a line(行的开头)
$ The end of a line(行的结尾)
\b A word boundary(单词边界)
\B A non-word boundary(非单词边界)
\A The beginning of the input(输入的开头)
\G The end of the previous match(上一个匹配的结尾)
\Z The end of the input but for the final terminator, if any(输入结尾,仅用于最后的结束符)
\z The end of the input(输入结尾)
public class Demo9_正则表达式 {
public static void main(String[] args) {
//需求:验证手机号码
String regex = "^1[35789]\\d{9}$";
String mobile = "13712341234";
boolean matches = mobile.matches(regex);
System.out.println(matches);
}
}
(6)常用的正则表达式
4、正则表达式的常用方法