正则表达式测试源码

本文提供了一个Java实现的正则表达式测试案例,演示了如何使用Pattern和Matcher类进行字符串匹配。通过实例展示了如何查找指定模式并获取匹配项的位置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

正则表达式测试源码

import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class RegexTestHarness {

// Sample
// Enter your regex: a{3}
// Enter input string to search: aaaaaaaaa
// I found the text "aaa" starting at index 0 and ending at index 3.
// I found the text "aaa" starting at index 3 and ending at index 6.
// I found the text "aaa" starting at index 6 and ending at index 9.
public static void main(String[] args) {
// RegEx 语法
String regex = "a{3}";
String check = "aaaaaaaaaa";
regex = "^R:323.\\d+";
check = "R:323.234";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(check);

boolean found = false;
while (matcher.find()) {
String msg = String.format("I found the text \"%s\" starting at " + "index %d and ending at index %d.%n", matcher
.group(), matcher.start(), matcher.end());
found = true;
System.out.println("find【" + found + "】:" + msg);
}
if (!found) {
System.out.println("Not matched !!!");
}
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值