检查字符串中是否包含变量str + int + int的序列

T. *_*ais 0 python substring

Python noob在这里。我有以下问题,有一个字符串包含这样的一系列情节:"whatever S01E02 sowhat" 其中系列(01)可以假定任何值(01至99),情节(02)也可以(01至99)..和I想要在字符串中找到它。我想使用一种聪明的方法,例如,如果string包含str(S)+int+int+str(E)+int+intthen 序列,但是我所实现的只是一个包含所有可能的系列(代码中的ij)和情节(代码中的kl)的列表,并循环创建一个清单...

如果字符串包含序列,有人知道如何进行此验证alphabetic+int+int+alphabetic+int+int吗?

我发现以下文章 Python是否具有字符串“包含”子字符串方法? 查找子字符串,但我无法适应想要的东西。

我想搜索一个子串("S"+int+int+"E"+int+int),其中这些int可能以巧妙的方式具有任何整数值。

下面是我实现的代码:

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)
Run Code Online (Sandbox Code Playgroud)

Joh*_*per 5

您可以为此使用正则表达式。以下将查找字符串中所有出现的内容:

import re 

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

有关正则表达式的更多信息可以在这里找到:https : //docs.python.org/3/howto/regex.html