regex - how to recognise a pattern until a second one is found
Question: I have a file, named a particular way. Let's say it's:

    
    
    tv_show.s01e01.episode_name.avi
    

it's the standard way a video file of a tv show's episode is named on the net.
The pattern is quite the same all over the web, so I want to extract some
information from a file named this way. Basically I want to get:

  1. the show's title;
  2. the season number `s01`;
  3. the episode number `e01`;
  4. the extension.

I'm using a Python 3 script to do so. This test file is pretty simple because
all I have to do is this

    
    
    import re
    
    def acquire_info(f="tv_show.s01e01.episode_name.avi"):
        tvshow_title = title_p.match(f).group()
        numbers = numbers_p.search(f).group()
        season_number = numbers.split("e")[0].split("s")[1]
        ep_number = numbers.split("e")[1]
        return [tvshow_title, season_number, ep_number]
    
    if __name__ == '__main__':
        # re.I stands for the option "ignorecase"
        title_p = re.compile("^[a-z]+", re.I)
        numbers_p = re.compile("s\d{1,2}e\d{1,2}", re.I)
        print(acquire_info())
    

and the output is as expected `['tv_show', '01', '01']`. But what if my file
name is like this other one? `some.other.tv.show.s04e05.episode_name.avi`.

How can I build a regex that gets all the text **BEFORE** the
`"s\d{1,2}e\d{1,2}"` pattern is found?

P.S. I didn't put in the example the code to get the extension, I know, but
that's not my problem so it does not matter.


Answer: try this

    
    
    show_p=re.compile("(.*)\.s(\d*)e(\d*)")
    show_p.match(x).groups()
    

where x is your string

Edit** (I forgot to include the extension, here is the revision)

    
    
    show_p=re.compile("^(.*)\.s(\d*)e(\d*).*?([^\.]*)$")
    show_p.match(x).groups()
    

* * *

And Here is the test result

    
    
    >>> show_p=re.compile("(.*)\.s(\d*)e(\d*).*?([^\.]*)$")
    >>> x="tv_show.s01e01.episode_name.avi"
    >>> show_p.match(x).groups()
    ('tv_show', '01', '01', 'avi')
    >>> x="tv_show.s2e1.episode_name.avi"
    >>> show_p.match(x).groups()
    ('tv_show', '2', '1', 'avi')
    >>> x='some.other.tv.show.s04e05.episode_name.avi'
    >>> show_p.match(x).groups()
    ('some.other.tv.show', '04', '05', 'avi')
    >>>  
    

