java 正则 删除,在Java中使用正则表达式匹配后删除部分字符串

I want to remove a part of a string following what matches my regex.

I am trying to make a TV show organization program and I want to cut off anything in the name following the season and episode marker in the form SXXEXX where X is a digit.

I grasped the regex model fairly easily to create "[Ss]\d\d[Ee]\d\d" which should match properly.

I want to use the Matcher method end() to get the last index in the string of the match but it does not seem to be working as I think it should.

Pattern p = Pattern.compile("[Ss]\\d\\d[Ee]\\d\\d");

Matcher m = p.matcher(name);

if(m.matches())

return name.substring(0, m.end());

If someone could tell me why this doesn't work and suggest a proper way to do it, that would be great. Thanks.

解决方案

matches() tries to match the whole string again the pattern. If you want to find your pattern within a string, use find(), find() will search for the next match in the string.

Your code could be quite the same:

if(m.find())

return name.substring(0, m.end());

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值