java 正则 reset_如何重置正则表达式的模式?(How to reset the pattern of a regular expression?)...

如何重置正则表达式的模式?(How to reset the pattern of a regular expression?)

问题描述 (Problem Description)

如何重置正则表达式的模式?

解决方案 (Solution)

下面的示例演示如何使用Pattern类的Pattern.compile()和Matcher类的m.find()方法重置正则表达式的模式。import java.util.regex.Matcher;

import java.util.regex.Pattern;

public class Resetting {

public static void main(String[] args) throws Exception {

Matcher m = Pattern.compile("[frb][aiu][gx]").matcher("fix the rug with bags");

while (m.find())System.out.println(m.group());

m.reset("fix the rig with rags");

while (m.find())System.out.println(m.group());

}

}

结果 (Result)

上面的代码示例将产生以下结果。fix

rug

bag

fix

rig

rag

以下是重置正则表达式模式的另一个示例示例import java.util.regex.Matcher;

import java.util.regex.Pattern;

public class Matc {

public static void main(String args[]) {

Pattern p = Pattern.compile("\\d");

Matcher mat1 = p.matcher("9652018244");

while (mat1.find()) {

System.out.println("\t\t" + mat1.group());

}

mat1.reset();

System.out.println("After done resetting the Matcher, it should be like this");

while (mat1.find()) {

System.out.println("\t\t" + mat1.group());

}

}

}

结果 (Result)

上面的代码示例将产生以下结果。9

6

5

2

0

1

8

2

4

4

After done resetting the Matcher, it should be like this

9

6

5

2

0

1

8

2

4

4

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值