正则表达式 Pattern 、Matcher

xiaoxiao2021-02-28  86

一 Pattern和Matcher

1.1 相关

在使用正则表达式之前我们需了解Pattern和Matcher,为什么要了解他们呢? 他们用来解决String不能解决的问题,是很实用切强大的正则表达式对象

1.2 类库

他们同属于一个类库包:java.util.regex

1.3 使用流程

关于流程,为了直观我画了一张图,我们看图说: 图中标出了3个模块,从上到下分别用1、2、3来表示 先说1:根据String类型的正则表达式生成Pattern对象,例如下面的 FIND_PATTERN

/** * @author transientba * 在使用正则表达式时,利用好其预编译功能,可以有效加快正则匹配速度。 * 说明:不要在方法体内定义:Pattern pattern = Pattern.compile(规则); */ private static Pattern FIND_PATTERN = Pattern.compile("\\w+");

再说2:根据Pattern的Matcher()方法,将要匹配的字符串传入,生成Matcher对象

Matcher matcher = FIND_PATTERN.matcher("Let's keep moving");

marcher对象一句pattern对象作为匹配模式对字符串展开匹配

最后说3:根据Matcher的不同方法得到不同的结果,个人理解他更像是在匹配后的基础上进行字符串的操作

while (matcher.find()) { System.out.print(matcher.group()+" "); }

输出结果如下:

Let s keep moving

二 正则表达式

2.1 上面的例子演示了如何使用Pattern和Matcher对字符串进行匹配操作,接下来我们看看正则:

main里面

public static void main(String[] args){ String str1 = "Hello~ my n ame is transientba"; String str2 = "Hello, X or XX, I 3 say transientba"; splitStringOne(str1); splitStringTwo(str2); }

splitStringOne和splitStringTwo两个方法

public static void splitStringOne(String str){ System.out.println("开始splitStringOne"); //以空格分割 System.out.println(Arrays.toString(str.split("\\s"))); //以spring分割 System.out.println(Arrays.toString(str.split("spring"))); //以非单词分割 ^A-Za-z0-9_ System.out.println(Arrays.toString (str.split("\\W+"))); } public static void splitStringTwo(String str){ System.out.println("开始splitStringTwo"); //以两个X分割 System.out.println(Arrays.toString(str.split("X{2}"))); //以数字分割 System.out.println(Arrays.toString(str.split("\\d"))); //以非数字分割 System.out.println(Arrays.toString(str.split("\\D"))); }

输出结果如下

开始splitStringOne [Hello~, my, n, ame, is, transientba] [Hello~ my n ame is transientba] [Hello, my, n, ame, is, transientba] 开始splitStringTwo [Hello, X or , , I 3 say transientba] [Hello, X or XX, I , say transientba] [, , , , , , , , , , , , , , , , , , 3]

三 用法

下面看看用法,我先在类中定义了两个Pattern(多个matcher可以共用一个Pattern),下面的方法将应用这两个Pattern

/** * @author transientba * 在使用正则表达式时,利用好其预编译功能,可以有效加快正则匹配速度。 * 说明:不要在方法体内定义:Pattern pattern = Pattern.compile(规则); */ private static Pattern FIND_PATTERN = Pattern.compile("\\w+"); private static Pattern REPLACE_PATTERN = Pattern.compile("moving");

3.1 find方法

先看代码和运行结果,然后解释

public static void demoFind(){ Matcher matcher = FIND_PATTERN.matcher("Let's keep moving"); System.out.println(matcher); while (matcher.find()) { System.out.print(matcher.group()+" "); } }

运行结果:

Let s keep moving

这里主要用到了find和group,这两个分别代表什么?matches()与find()的区别?

3.1.1 find()

是对字符串部分匹配,查找输入的字符串与规定的Pattern规则模式匹配的子字符串,如果匹配的字符串有组则可使用group()

3.1.2 matchers()

是对字符串全部匹配,比如验证一个字符串是否为电话号码,是否符合密码规则等

3.2 find(int i)方法

/** * @author transientba * find(int i) */ public static void demoFindInt(){ Matcher matcher = FIND_PATTERN.matcher("Let's keep moving"); int i = 0; //重置此匹配器,然后尝试查找匹配该模式、从指定索引开始的输入序列的下一个子序列。 while (matcher.find(i)) { System.out.print(matcher.group()+" "); i++; } }

输出结果:

Let et t s s keep keep eep ep p moving moving oving ving ing ng g

3.3 star 和 end 方法

/** * @author transientba * Star 和 End */ public static void doStarAndEnd(){ Matcher matcher = FIND_PATTERN.matcher("Let's keep moving"); while (matcher.find()) { System.out.println(matcher.group() + " " + matcher.start() + " " + matcher.end()); } }

输出结果:

Let 0 3 s 4 5 keep 6 10 moving 11 17

3.4 replaceFirst方法

/** * @author transientba * demoReplaceFirst */ public static void demoReplaceFirst(){ Matcher matcher = REPLACE_PATTERN.matcher("Let's keep moving"); String replaceFirstString = matcher.replaceFirst("eating"); System.out.println(replaceFirstString); }

运行结果:

Let's keep eatingLet's keep eating

3.5 replaceAll方法

/** * @author transientba * demoReplaceAll */ public static void demoReplaceAll(){ Matcher matcher = REPLACE_PATTERN.matcher("let's keep moving moving"); String replaceAll = matcher.replaceAll("eating"); System.out.println(replaceAll); }

输出结果:

let's keep eating eating

3.6 appendReplacement方法

/** * @author transientba * 匹配项替换后 后面的会被断掉 * appendReplacement */ public static void domoAppendReplacement(){ Matcher matcher = REPLACE_PATTERN.matcher("let's keep moving moving gogogogogo"); StringBuffer buffer = new StringBuffer(); while(matcher.find()){ matcher.appendReplacement(buffer, "eating?"); } System.out.println(buffer.toString()); }

输出结果: 注意 原本在第二个moving后面的gogogogo被截断了

let's keep eating? eating?

3.7 appendTail方法

/** * @author transientba * 匹配项替换后 匹配后剩余结果追加到StringBuffer后 后面的不会被断掉 * appendTail */ public static void demoAppendTail(){ Matcher matcher = REPLACE_PATTERN.matcher("let's keep moving moving gogogogogo"); StringBuffer buffer = new StringBuffer(); while(matcher.find()){ matcher.appendReplacement(buffer, "eating?"); } matcher.appendTail(buffer); System.out.println(buffer.toString()); }

输出结果:

let's keep eating? eating? gogogogogo

四 其他正则语法

语法引自:http://www.runoob.com/regexp/regexp-syntax.html

4.1 非打印字符

字符描述\cx匹配由x指明的控制字符。例如, \cM 匹配一个 Control-M 或回车符。x 的值必须为 A-Z 或 a-z 之一。否则,将 c 视为一个原义的 ‘c’ 字符。\f匹配一个换页符。等价于 \x0c 和 \cL。\n匹配一个换行符。等价于 \x0a 和 \cJ 。\r匹配一个回车符。等价于 \x0d 和 \cM。\s匹配任何空白字符,包括空格、制表符、换页符等等。等价于 [ \f\n\r\t\v]。\S匹配任何非空白字符。等价于 [^ \f\n\r\t\v]。\t匹配一个制表符。等价于 \x09 和 \cI。\v匹配一个垂直制表符。等价于 \x0b 和 \cK。

4.2 特殊字符

字符描述 |RegExpMultiline 也匹配 ‘\n’ 或 ‘\r’。要匹配 $ 字符本身,请使用 \$。( )标记一个子表达式的开始和结束位置。子表达式可以获取供以后使用。要匹配这些字符,请使用 ( 和 )。*标记一个子表达式的开始和结束位置。子表达式可以获取供以后使用。要匹配这些字符,请使用 ( 和 )。+匹配前面的子表达式一次或多次。要匹配 + 字符,请使用 +。.匹配除换行符 \n 之外的任何单字符。要匹配 . ,请使用 . 。[标记一个中括号表达式的开始。要匹配 [,请使用 [。?匹配前面的子表达式零次或一次,或指明一个非贪婪限定符。要匹配 ? 字符,请使用 \?。| 将下一个字符标记为或特殊字符、或原义字符、或向后引用、或八进制转义符。例如, ‘n’ 匹配字符 ‘n’。’\n’ 匹配换行符。序列 ‘\’ 匹配 “\”,而 ‘(’ 则匹配 “(“。^匹配输入字符串的开始位置,除非在方括号表达式中使用,此时它表示不接受该字符集合。要匹配 ^ 字符本身,请使用 \^。{标记限定符表达式的开始。要匹配 {,请使用 {。

4.3 限定符

字符描述*匹配前面的子表达式零次或多次。例如,zo* 能匹配 “z” 以及 “zoo”。* 等价于{0,}。+匹配前面的子表达式一次或多次。例如,’zo+’ 能匹配 “zo” 以及 “zoo”,但不能匹配 “z”。+ 等价于 {1,}。?匹配前面的子表达式零次或一次。例如,”do(es)?” 可以匹配 “do” 、 “does” 中的 “does” 、 “doxy” 中的 “do” 。? 等价于 {0,1}。{n}n 是一个非负整数。匹配确定的 n 次。例如,’o{2}’ 不能匹配 “Bob” 中的 ‘o’,但是能匹配 “food” 中的两个 o。{n,}n 是一个非负整数。至少匹配n 次。例如,’o{2,}’ 不能匹配 “Bob” 中的 ‘o’,但能匹配 “foooood” 中的所有 o。’o{1,}’ 等价于 ‘o+’。’o{0,}’ 则等价于 ‘o*’。{n,m}m 和 n 均为非负整数,其中n <= m。最少匹配 n 次且最多匹配 m 次。例如,”o{1,3}” 将匹配 “fooooood” 中的前三个 o。’o{0,1}’ 等价于 ‘o?’。请注意在逗号和两个数之间不能有空格。

4.4 定位符

字符描述^匹配输入字符串开始的位置。如果设置了 RegExp 对象的 Multiline 属性,^ 还会与 \n 或 \r 之后的位置匹配。 |RegExpMultiline 还会与 \n 或 \r 之前的位置匹配。\b匹配一个字边界,即字与空格间的位置。\B非字边界匹配。

注意:不能将限定符与定位符一起使用。由于在紧靠换行或者字边界的前面或后面不能有一个以上位置,因此不允许诸如 ^* 之类的表达式。

五 本篇源码

package com.example.domain; import java.util.Arrays; import java.util.Date; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * @author transientba * @date 2018/01/08 */ public class StarString { public static void splitStringOne(String str){ System.out.println("开始splitStringOne"); //以空格分割 System.out.println(Arrays.toString(str.split("\\s"))); //以spring分割 System.out.println(Arrays.toString(str.split("spring"))); //以非单词分割 ^A-Za-z0-9_ System.out.println(Arrays.toString (str.split("\\W+"))); } public static void splitStringTwo(String str){ System.out.println("开始splitStringTwo"); //以两个X分割 System.out.println(Arrays.toString(str.split("X{2}"))); //以数字分割 System.out.println(Arrays.toString(str.split("\\d"))); //以非数字分割 System.out.println(Arrays.toString(str.split("\\D"))); } /** * @author transientba * 在使用正则表达式时,利用好其预编译功能,可以有效加快正则匹配速度。 * 说明:不要在方法体内定义:Pattern pattern = Pattern.compile(规则); */ private static Pattern FIND_PATTERN = Pattern.compile("\\w+"); private static Pattern REPLACE_PATTERN = Pattern.compile("moving"); /** * @author transientba * find */ public static void demoFind(){ Matcher matcher = FIND_PATTERN.matcher("Let's keep moving"); //尝试查找与该模式匹配的输入序列的的下一个子序列。 while (matcher.find()) { System.out.print(matcher.group()+" "); } } /** * @author transientba * find(int i) */ public static void demoFindInt(){ Matcher matcher = FIND_PATTERN.matcher("Let's keep moving"); int i = 0; //重置此匹配器,然后尝试查找匹配该模式、从指定索引开始的输入序列的下一个子序列。 while (matcher.find(i)) { System.out.print(matcher.group()+" "); i++; } } /** * @author transientba * Star 和 End */ public static void doStarAndEnd(){ Matcher matcher = FIND_PATTERN.matcher("Let's keep moving"); while (matcher.find()) { System.out.println(matcher.group() + " " + matcher.start() + " " + matcher.end()); } } /** * @author transientba * demoReplaceFirst */ public static void demoReplaceFirst(){ Matcher matcher = REPLACE_PATTERN.matcher("Let's keep moving"); String replaceFirstString = matcher.replaceFirst("eating"); System.out.println(replaceFirstString); } /** * @author transientba * demoReplaceAll */ public static void demoReplaceAll(){ Matcher matcher = REPLACE_PATTERN.matcher("let's keep moving moving"); String replaceAll = matcher.replaceAll("eating"); System.out.println(replaceAll); } /** * @author transientba * 匹配项替换后 后面的会被断掉 * appendReplacement */ public static void domoAppendReplacement(){ Matcher matcher = REPLACE_PATTERN.matcher("let's keep moving moving gogogogogo"); StringBuffer buffer = new StringBuffer(); while(matcher.find()){ matcher.appendReplacement(buffer, "eating?"); } System.out.println(buffer.toString()); } /** * @author transientba * 匹配项替换后 匹配后剩余结果追加到StringBuffer后 后面的不会被断掉 * appendTail */ public static void demoAppendTail(){ Matcher matcher = REPLACE_PATTERN.matcher("let's keep moving moving gogogogogo"); StringBuffer buffer = new StringBuffer(); while(matcher.find()){ matcher.appendReplacement(buffer, "eating?"); } matcher.appendTail(buffer); System.out.println(buffer.toString()); } public static void main(String[] args){ String str1 = "Hello~ my n ame is transientba"; String str2 = "Hello, X or XX, I 3 say transientba"; splitStringOne(str1); splitStringTwo(str2); demoFind(); demoFindInt(); doStarAndEnd(); demoReplaceFirst(); demoReplaceAll(); domoAppendReplacement(); demoAppendTail(); } }
转载请注明原文地址: https://www.6miu.com/read-1450263.html

最新回复(0)