首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

正则表达式-JAVA成长之路

Pattern 类:

pattern 对象是一个正则表达式的编译表示。Pattern 类没有公共构造方法。要创建一个 Pattern 对象,你必须首先调用其公共静态编译方法,它返回一个 Pattern 对象。该方法接受一个正则表达式作为它的第一个参数。

Matcher 类:

Matcher 对象是对输入字符串进行解释和匹配操作的引擎。与Pattern 类一样,Matcher 也没有公共构造方法。你需要调用 Pattern 对象的 matcher 方法来获得一个 Matcher 对象。

PatternSyntaxException:

PatternSyntaxException 是一个非强制异常类,它表示一个正则表达式模式中的语法错误。

以下实例中使用了正则表达式.*runoob.*用于查找字符串中是否包含了runoob子串:

1packagecc.bcy;

2

3importjava.util.regex.*;

4

5publicclassRegexExample

6{

7publicstaticvoidmain(String[] args)

8{

9String content="I am noob from runoob.com";

10String pattern=".*runoob.*";

11booleanisMatch=Pattern.matches(pattern, content);

12System.out.println("字符串中是否包含了‘runoob’子字符串?"+isMatch);

13}

14}

15/*

16字符串中是否包含了‘runoob’子字符串?true

17*/

捕获组:

捕获组是把多个字符当一个单独单元进行处理的方法,它通过对括号内的字符分组来创建。可以通过调用 matcher 对象的 groupCount 方法来查看表达式有多少个分组。groupCount 方法返回一个 int 值,表示matcher对象当前有多个捕获组。

还有一个特殊的组(group(0)),它总是代表整个表达式。该组不包括在 groupCount 的返回值中。

1packagecc.bcy;

2

3importjava.util.regex.*;

4

5publicclassRegexExample

6{

7publicstaticvoidmain(String[] args)

8{

9String line="This order was placed for QT3000! OK?";

10String pattern="(\\D*)(\\d+)(.*)";

11//创建Pattern对象

12Pattern p=Pattern.compile(pattern);

13//创建Matcher对象

14Matcher m=p.matcher(line);

15if(m.find())

16{

17System.out.println("Found value: "+m.group(0));

18System.out.println("Found value: "+m.group(1));

19System.out.println("Found value: "+m.group(2));

20System.out.println("Found value: "+m.group(3));

21}

22else

23{

24System.out.println("No Match!");

25}

26intn=m.groupCount();

27System.out.println("一共有"+n+"个捕获组");

28}

29}

30/*

31Found value: This order was placed for QT3000! OK?

32Found value: This order was placed for QT

33Found value: 3000

34Found value: ! OK?

35一共有3个捕获组

36*/

Java 正则表达式语法:

在其他语言中,\\ 表示:我想要在正则表达式中插入一个普通的(字面上的)反斜杠,请不要给它任何特殊的意义。

在 Java 中,\\ 表示:我要插入一个正则表达式的反斜线,所以其后的字符具有特殊的意义。

所以,在其他的语言中(如Perl),一个反斜杠 \ 就足以具有转义的作用,而在 Java 中正则表达式中则需要有两个反斜杠才能被解析为其他语言中的转义作用。也可以简单的理解在 Java 的正则表达式中,两个 \\ 代表其他语言中的一个 \,这也就是为什么表示一位数字的正则表达式是 \\d,而表示一个普通的反斜杠是 \\\\。

Matcher 类的方法:

下面是一个对单词 "cat" 出现在输入字符串中出现次数进行计数的例子:

1packagecc.bcy;

2

3importjava.util.regex.*;

4

5publicclassRegexExample

6{

7privatestaticfinalString REGEX="\\bcat\\b";

8privatestaticfinalString INPUT="cat cat cat cattie cat";

9publicstaticvoidmain(String[] args)

10{

11Pattern p=Pattern.compile(REGEX);

12Matcher m=p.matcher(INPUT);

13intcount=0;

14while(m.find())

15{

16count++;

17System.out.println("Match number "+count);

18System.out.println("start() "+m.start());

19System.out.println("end() "+m.end());

20System.out.println();

21}

22}

23}

24/*

25Match number 1

26start() 0

27end() 3

28

29Match number 2

30start() 4

31end() 7

32

33Match number 3

34start() 8

35end() 11

36

37Match number 4

38start() 19

39end() 22

40*/

matches 和 lookingAt 方法都用来尝试匹配一个输入序列模式。它们的不同是 matches 要求整个序列都匹配,而lookingAt 不要求。lookingAt 方法虽然不需要整句都匹配,但是需要从第一个字符开始匹配。

把字符串中的匹配项全部替换成“-”:(用appendReplacement()方法)

1packagecc.bcy;

2

3importjava.util.regex.*;

4

5publicclassRegexExample

6{

7privatestaticfinalString REGEX="a*b";

8privatestaticfinalString INPUT="aabfooaabfooabfoobkkk";

9publicstaticvoidmain(String[] args)

10{

11Pattern p=Pattern.compile(REGEX);

12Matcher m=p.matcher(INPUT);

13StringBuffer sb=newStringBuffer();

14while(m.find())

15{

16m.appendReplacement(sb, "-");

17}

18m.appendTail(sb);

19System.out.println(sb.toString());

20}

21}

22/*

23-foo-foo-foo-kkk

24*/

replaceFirst 和 replaceAll 方法用来替换匹配正则表达式的文本。不同的是,replaceFirst 替换首次匹配,replaceAll 替换所有匹配。

1packagecc.bcy;

2

3importjava.util.regex.*;

4

5publicclassRegexExample

6{

7privatestaticfinalString REGEX="dog";

8privatestaticfinalString INPUT="The dog says meow. All dogs say meow.";

9publicstaticvoidmain(String[] args)

10{

11Pattern p=Pattern.compile(REGEX);

12Matcher m=p.matcher(INPUT);

13String str=m.replaceAll("cat");

14System.out.println(str);

15}

16}

17/*

18The cat says meow. All cats say meow.

19*/

PatternSyntaxException 类的方法:

PatternSyntaxException 是一个非强制异常类,它指示一个正则表达式模式中的语法错误。 PatternSyntaxException 类提供了下面的方法来帮助我们查看发生了什么错误。

  • 发表于:
  • 原文链接https://kuaibao.qq.com/s/20200630A06B9U00?refer=cp_1026
  • 腾讯「腾讯云开发者社区」是腾讯内容开放平台帐号(企鹅号)传播渠道之一,根据《腾讯内容开放平台服务协议》转载发布内容。
  • 如有侵权,请联系 cloudcommunity@tencent.com 删除。

扫码

添加站长 进交流群

领取专属 10元无门槛券

私享最新 技术干货

扫码加入开发者社群
领券