java startswith 正则_在Java中使用正则表达式进行部分精确匹配(Partly exact matching using regex in Java)...

I need to find if a String starts with "abcd" followed by 1-5 digits, then a comma, then ends with 0-3 digits.

Pattern pattern = Pattern.compile("abcd[0-9]{1,5},[0-9]{0,3}$");

String[] data = { "pqrsabcd12345,5", "abcd1234,5", "abcd1234542155,",

"abcdSD12345,555", "abcd123,555", "abcd12,5555",

"abcd,5555ffdfd", "abcd2,5555ffdfd", "abcd2,5" };

for (CharSequence input : data) {

Matcher matcher = pattern.matcher(input);

while (matcher.find()) {

System.out.format("\nI found the text %s :"

+ " \"%s\" starting at "

+ "index %d and ending at index %d.%n", input,

matcher.group(), matcher.start(), matcher.end());

}

}

The output :

I found the text pqrsabcd12345,5 : "abcd12345,5" starting at index 4 and ending at index 15.

I found the text abcd1234,5 : "abcd1234,5" starting at index 0 and ending at index 10.

I found the text abcd123,555 : "abcd123,555" starting at index 0 and ending at index 11.

I found the text abcd2,5 : "abcd2,5" starting at index 0 and ending at index 7.

Using this, I could ensure the ends with part. I think I am left with stopping Strings like "pqrsabcd12345,5"

Please let me know if I have missed something.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值