Java正则表达式
来源:互联网 发布:外包美工加班多吗 编辑:程序博客网 时间:2024/05/21 19:39
一、正则表达式是什么
正则表达式即定义了符合一定规范的字符串,用来约束给定字符串的形式,通过一定的模式来操作给定的字符串。常见的操作有匹配、分割和替换等操作。
对字符串的操作一般用java.util.regex包下的Pattern和Matcher来配合操作。通常用的String类对字符串的正则操作内部也是用的前面两者来实现的。
二、 常用的匹配规则
1 字符
2 字符类
3 预定义字符类
4 边界匹配器
5 Greedy 数量词
6 特殊构造(非捕获)
7 反斜线、转义和引用
反斜线字符 (‘\’) 用于引用转义构造,如上表所定义的,同时还用于引用其他将被解释为非转义构造的字符。因此,表达式 \\ 与单个反斜线匹配,而 \{ 与左括号匹配。
在不表示转义构造的任何字母字符前使用反斜线都是错误的;它们是为将来扩展正则表达式语言保留的。可以在非字母字符前使用反斜线,不管该字符是否非转义构造的一部分。
根据 Java Language Specification 的要求,Java 源代码的字符串中的反斜线被解释为 Unicode 转义或其他字符转义。因此必须在字符串字面值中使用两个反斜线,表示正则表达式受到保护,不被 Java 字节码编译器解释。例如,当解释为正则表达式时,字符串字面值 “\b” 与单个退格字符匹配,而 “\b” 与单词边界匹配。字符串字面值 “(hello)” 是非法的,将导致编译时错误;要与字符串 (hello) 匹配,必须使用字符串字面值 “\(hello\)”。
8 字符类
字符类可以出现在其他字符类中,并且可以包含并集运算符(隐式)和交集运算符 (&&)。并集运算符表示至少包含其某个操作数类中所有字符的类。交集运算符表示包含同时位于其两个操作数类中所有字符的类。
字符类运算符的优先级如下所示,按从最高到最低的顺序排列:
注意,元字符的不同集合实际上位于字符类的内部,而非字符类的外部。例如,正则表达式 . 在字符类内部就失去了其特殊意义,而表达式 - 变成了形成元字符的范围。
9 组和捕获
捕获组可以通过从左到右计算其开括号来编号。例如,在表达式 ((A)(B(C))) 中,存在四个这样的组:
引用自jdk文档
之所以这样命名捕获组是因为在匹配中,保存了与这些组匹配的输入序列的每个子序列。捕获的子序列稍后可以通过 Back 引用在表达式中使用,也可以在匹配操作完成后从匹配器获取。
使用分组
1、将多个重复的字母替换成一个
// 替换 String regex = "(.)\\1+"; String str = "helllo wwwworrrldddddd"; String newStr = str.replaceAll(regex, "$1"); // 重复替换为一个 System.out.println(newStr); // output : helo world
2、按多个重复的字符来分割
// 分组 进行重叠子切割 String s = "sddddvdfsssdfgcccgerrrd"; String regex2 = "(.)\\1+"; String[] ss = s.split(regex2); // 按重复元素进行分割 for (String string : ss) { System.out.print(string+" "); //分割 } // output : s vdf dfg ge d
三、常用功能
1、匹配(match)
1 String类
public boolean matches(String regex)
代码示例
``` String regex = ".+"; // 匹配多个任意字符 String str = "hi,regex~"; System.out.println(str.matches(regex));```
2 Pattern与Marcher类结合使用
Pattern p = Pattern.compile("a*b"); Matcher m = p.matcher("aaaaab"); boolean b = m.matches();
3 Pattern类
static boolean matches(String regex, CharSequence input) 编译给定正则表达式并尝试将给定输入与其匹配。
代码示例
boolean b = Pattern.matches("\\w+", "hello regex"); // 匹配单词字符:[a-zA-Z_0-9]
等效于上面的三个语句,尽管对于重复的匹配而言它效率不高,因为它不允许重用已编译的模式。
此类的实例是不可变的,可供多个并发线程安全使用。Matcher 类的实例用于此目的则不安全。
2 分割(split)
1 String类
public String[] split(String regex)
public String[] split(String regex,int limit)
代码示例
String regex = "\\."; // 按点进行分切String str = "com.yu.regex";String[] ss = str.split(regex);//String[] ss = str.split(regex,2); // 限定分割的数组长度for (String string : ss) { System.out.print(string+" "); // output : com yu regex }
2 Pattern类
String[] split(CharSequence input) 围绕此模式的匹配拆分给定输入序列。
String[] split(CharSequence input, int limit) 围绕此模式的匹配拆分给定输入序列。
代码示例
– split(CharSequence input)
String regex = ",";String str = "Where did you go?,I miss you,baby";Pattern p = Pattern.compile(regex);String[] s = p.split(str);
– split(CharSequence input, int limit)
limit限定数组的长度,①limit<=0时,能拆分多少数组长度就为多少,②limit>0时,数组长度为limit的大小,当可以拆分的长度大于limit时,大于limit的部分不再拆分,当可拆分成的数组长度小于limit时,后面补""使数组长度为limit
String regex = "(.)\\1+"; // 按组分割 String str = "uhyhij&&&knjijnk****jkbj---hjgfmj"; Pattern p = Pattern.compile(regex); String[] ss = p.split(str,3); // The limit parameter controls the number of times the pattern is applied and therefore affects the length of the resulting array. for (String string : ss) { System.out.print(string+" "); //分割 uhyhij knjijnk jkbj---hjgfmj ,limit=3,可拆分的长度为4大于3,最后一个不再拆分。 }
3 替换(replace)
String类
public String replaceAll(String regex,String replacement)
代码示例
String regex = " ";String str = "hello world";String newStr = str.replaceAll(regex, "qq");System.out.println(newStr); //output: helloqqworld
Matcher类
public String replaceAll(String replacement) // 按Pattern中编译的正则表达式进行替换
String str = "helllo wwwworrrldddddd";String regex = "(.)\\1+"; // 匹配多个重复字符Pattern p=Pattern.compile(regex);Matcher matcher=p.matcher(str);String newStr=matcher.replaceAll("666"); //将匹配字符替换成666System.out.println(newStr); // output : he666o 666o666l666
4 获取
Matcher类
boolean find()
尝试查找与该模式匹配的输入序列的下一个子序列。
lookingAt()
尝试将从区域开头开始的输入序列与该模式匹配。只有当从开始匹配才会返回true
String group(int group)
返回在以前匹配操作期间由给定组捕获的输入子序列。
find代码示例
String str = "A hjadAaJjikdbASdsFmfKkl"; String regex = "([a-z][A-Z])+"; // ()表示分组,匹配多个由小写大写字母组成的组合, // 将正则表达式封装成对象,获取pattern对象 Pattern p = Pattern.compile(regex); // 通过正则对象获取匹配器 Matcher matcher = p.matcher(str);// System.out.println(matcher.group()); // find之前调用,抛出异常// System.out.println(matcher.find()); while(matcher.find()){ // 如果匹配找到 System.out.println(matcher.group()); // 获取匹配的子序列 System.out.println(matcher.start()+"::"+matcher.end()); // 获取匹配的子序列的开始结束索引 }
output:
dAaJ5::9bA13::15sF17::19fK20::22
注意: 要先同过find方法查找,再通过group方法获取,否则会抛异常
Exception in thread "main" java.lang.IllegalStateException: No match found ...
lookingAt() 代码示例
private static void find() { String str = "aAhjadAaJjikdbASdsFmfKkl"; // 匹配 String str = "a AhjadAaJjikdbASdsFmfKkl"; // 不匹配 String regex = "([a-z][A-Z])+"; // 将正则表达式封装成对象,获取pattern对象 Pattern p = Pattern.compile(regex); // 通过正则对象获取匹配器 Matcher matcher = p.matcher(str); boolean lookingAt = matcher.lookingAt(); System.out.println(lookingAt); }
四、 几个小例子
1、匹配qq号
private static void match2(){ String regex = "^[1-9][0-9]{4,14}$"; // 匹配qq String str = "1234567890658602"; System.out.println(str.matches(regex)); } // output : false
2、模拟将内容中的电话号码置为多个*(模拟屏蔽敏感信息)
private static void replcae4() { String str="这是我的电话号码:13843843888,可约可聊哟"; String regex="[0-9]"; String newStr = str.replaceAll(regex, "*******"); System.out.println(newStr); } // output : 这是我的电话号码:***********,可约可聊哟
3、匹配邮箱
private static void match3(){ // 匹配邮箱 // String regex = "^[a-zA-Z0-9]+@\\w+(\\.[a-z]+)+$"; String regex = "\\w+@\\w+(\\.[a-z]+)+"; String str = "117ytu4556@qq.com.cn"; System.out.println(Pattern.matches(regex, str)); } // output : true
- Java 正则表达式正则
- 【正则表达式】Java正则表达式
- 正则表达式 JAVA正则表达式
- 【Java】【正则表达式】正则表达式
- Java 正则表达式详解_正则表达式
- java正则表达式!正则表达式踩坑!!!
- 正则表达式工具类,正则表达式封装,Java正则表达式
- Java正则表达式详解
- Java正则表达式详解
- Java正则表达式详解
- Java正则表达式详解
- Java正则表达式详解
- java和正则表达式
- java正则表达式详解!
- Java正则表达式详解
- Java正则表达式详解
- Java正则表达式详解
- Java正则表达式详解
- python 文件操作read(),readline(),readlines()
- 个人IT常用网站资源记录(不定时更新)
- 第三方框架数据库xutils的简单用法
- prvf-0002错误
- 我的MYSQL学习心得(四) 数据类型
- Java正则表达式
- Android基础学习【历史流程重走】 ---- 四大组件之Service
- 九月十八日Oracle整理
- Java按值传递与按引用传递
- 浅谈ThreadLocal类
- 我的MYSQL学习心得(五) 运算符
- 【BZOJ4600】硬币游戏,博弈
- Varnish
- java 基础 5 集合(中)