使用 ?Java正则表达式中的量词
一般而言,量词表示指定模式出现0或1次。例如-X?表示X出现0或1。
正则表达式“t。+?m”用于使用?查找字符串“tom和tim是最好的朋友”中的匹配项。量词。
演示此过程的程序如下:
示例
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Demo {
public static void main(String args[]) {
Pattern p = Pattern.compile("t.+?m");
Matcher m = p.matcher("tom and tim are best friends");
System.out.println("The input string is: tom and tim are best friends");
System.out.println("The Regex is: t.+?m");
System.out.println();
while (m.find()) {
System.out.println("Match: " + m.group());
}
}
}输出结果
The input string is: tom and tim are best friends The Regex is: t.+?m Match: tom Match: tim
现在让我们了解上面的程序。
在字符串序列“tom和tim是最好的朋友”中搜索子序列“t。+?m”。该find()方法用于确定子序列(即t后面跟任意数量的t并以m结尾)是否在输入序列中,并且是否打印了所需的结果。
热门推荐
1 如何训练狗狗不叫
2 训练狗狗不叫
3 训练狗狗随行
4 怎么训练狗狗不叫
7 训练比熊狗狗教程
10 上海狗狗训练
11 怎么训练狗狗上厕所
12 训练狗狗趴下
13 狗狗服从训练
14 训练狗狗拒食
15 训练狗狗笑
16 训练狗狗大便
17 训练狗狗立
18 训练狗狗捡东西