0

Python noob here. I have the following issue, there is a string which contains an episode of a series like this: "whatever S01E02 sowhat" where series (01) can assume any value(01 until 99), and episode (02) also (01 until 99).. and I want to find this in the string. I would like to use a clever way like if string contains a sequence of str(S)+int+int+str(E)+int+int then... but all I did implement was a list containing all possible series (ij in the code) and episodes (kl in the code) and looped to create a list...

Does someone knows how to make this verification if a string contain a sequence of alphabetic+int+int+alphabetic+int+int?

I found the following post Does Python have a string 'contains' substring method? to find substrings, but I couldn't adapt to have what I want.

I would like to search for a substring ("S"+int+int+"E"+int+int) where these ints may have any integer value in a clever way.

below is my implemented code:

series_episode = "Series whatever S01E04 formating no-one-cares"
list_SijEkl = []
i,j,k,l=0,1,0,1
while i < 2:
    while j < 10:
        k,l=0,1
        while k<3:
            while l<10:

list_SijEkl.append("S"+str(i)+str(j)+'E'+str(k)+str(l))

                l+=1
            l=0
            k+=1
        j+=1
    i+=1

#print(list_SijEkl)

for episode in list_SijEkl:
    if episode in series_episode:
        cut = series_episode.split(episode)
        before = cut[0]
        after = cut[1]
        print('cut before '+ before)
        print('cut after'+ after)
        print (before + episode)
        print ('what i want in the end: '+before + episode)
Nazim Kerimbekov
  • 4,712
  • 8
  • 34
  • 58
T. Novais
  • 79
  • 8
  • 3
    You can use a [regular expression](https://docs.python.org/3/howto/regex.html) e.g. [`S(\d\d)E(\d\d)`](https://regex101.com/r/XXT7Dg/1) or [`S(\d+)E(\d+)`](https://regex101.com/r/T9fczF/1). – jdehesa Mar 21 '19 at 10:00

2 Answers2

5

You can use regex for this. The following will find all occurrences in the string:

import re 

s = "Series whatever S01E04 formating no-one-cares"
re.findall('.+(S[0-9]{2}E[0-9]{2}).+', s)

More info on regex can be found here: https://docs.python.org/3/howto/regex.html

John Sloper
  • 1,813
  • 12
  • 14
0

You can use try-except method in this case. You check each word in the line,then:

if len(word)==6:
     if word[0]=='S' and word[3]=='E':
          try:
              a=int(word[1:3])
              b=int(word[3:])
              print(word,' found')
          except:
              pass
Tojra
  • 673
  • 5
  • 20