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

时间:2021-10-31 21:45:27

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.

我正在尝试制作一个电视节目组织节目,我希望在SXXEXX形式的季节和剧集标记后切断名称中的任何内容,其中X是一个数字。

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

我很容易掌握正则表达式模型来创建“[Ss] \ d \ d [Ee] \ d \ d”,它应该正确匹配。

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.

我想使用Matcher方法end()来获取匹配字符串中的最后一个索引,但它似乎并没有像我认为的那样工作。

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.

如果有人能告诉我为什么这不起作用并建议一个正确的方法来做到这一点,那就太好了。谢谢。

3 个解决方案

#1


5  

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.

matches()尝试再次匹配整个字符串模式。如果要在字符串中查找模式,请使用find(),find()将搜索字符串中的下一个匹配项。

Your code could be quite the same:

你的代码可能完全相同:

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

#2


4  

matches matches the entire string, try find()

匹配整个字符串,尝试find()

You could capture the name as well:

您也可以捕获名称:

String name = "a movie S01E02 with some stuff";
Pattern p = Pattern.compile("(.*[Ss]\\d\\d[Ee]\\d\\d)");
Matcher m = p.matcher(name);

if (m.find())
    System.out.println(m.group());
else
    System.out.println("No match");

Will capture and print:

将捕获和打印:

a movie S01E02

电影S01E02

#3


1  

This should work

这应该工作

.*[Ss]\d\d[Ee]\d\d

In java (I'm rusty) this will be

在java(我生锈)这将是

String ResultString = null;

Pattern regex = Pattern.compile(".*[Ss]\\d\\d[Ee]\\d\\d");
Matcher regexMatcher = regex.matcher("Title S11E11Blah");
if (regexMatcher.find()) {
    ResultString = regexMatcher.group();
} 

Hope this helps

希望这可以帮助

#1


5  

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.

matches()尝试再次匹配整个字符串模式。如果要在字符串中查找模式,请使用find(),find()将搜索字符串中的下一个匹配项。

Your code could be quite the same:

你的代码可能完全相同:

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

#2


4  

matches matches the entire string, try find()

匹配整个字符串,尝试find()

You could capture the name as well:

您也可以捕获名称:

String name = "a movie S01E02 with some stuff";
Pattern p = Pattern.compile("(.*[Ss]\\d\\d[Ee]\\d\\d)");
Matcher m = p.matcher(name);

if (m.find())
    System.out.println(m.group());
else
    System.out.println("No match");

Will capture and print:

将捕获和打印:

a movie S01E02

电影S01E02

#3


1  

This should work

这应该工作

.*[Ss]\d\d[Ee]\d\d

In java (I'm rusty) this will be

在java(我生锈)这将是

String ResultString = null;

Pattern regex = Pattern.compile(".*[Ss]\\d\\d[Ee]\\d\\d");
Matcher regexMatcher = regex.matcher("Title S11E11Blah");
if (regexMatcher.find()) {
    ResultString = regexMatcher.group();
} 

Hope this helps

希望这可以帮助