简体   繁体   中英

Regex match up to the end of a standard pattern

I'm working on an application to manage filenames of downloaded TV Shows. Basically it will search the directory and clean up the filenames, removing things like full stops and replacing them with spaces and getting rid of the descriptions at the end of the filename after the easily recognizable pattern of, for eg., S01E13. (.1080p.BluRay.x264-ROVERS)

What I want to do is to make a regex expression for use in C# to just extract whatever is before the SnnEnn including itself (where n is any whole positive integer). But, i don't know much regex to get me going

For example, if I had the filename TV.Show.S01E01.1080p.BluRay.x264-ROVERS, the query would only get TV.Show.S01E01, irrespective of how many words are before the pattern, so it could be TV.Show.On.ABC.S01E01 and it would still work.

Thanks for any help :)

Try this

string input = "TV.Show.S01E01.1080p.BluRay.x264-ROVERS";
            string pattern = @"(?'pattern'^.*\d\d[A-Z]\d\d)";
            string results = Regex.Match(input, pattern).Groups["pattern"].Value;

There is more obvious way without regex:

string GetNameByPattern(string s)
{
    const string pattern_length = 6; //SnnEnn

    for (int i = 0; i < s.Length - pattern_length; i++)
    {
        string part = s.SubString(i, pattern_length);

        if (part[0] == 'S' && part[3] == 'N') //candidat
            if (Char.IsDigit(part[1]) && Char.IsDigit(part[2]) && Char.IsDigit(part[4]) && Char.IsDigit(part[5])) 
                return s.SubString(0, i + pattern_length);
    }

    return "";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM