Edited, final version
Meets 2 templates with one regex.
This version results in a tuple with numerical values. As the regex meets the 2 templates, the tuple will always return 4 items, 2 of them None
, unless the string is the sum of the two templates. You can also take the values of the groups separately, that is, to know what was between the brackets (template 1), consider the groups: 'Bracket' and 'Point'. For the second template take the groups’S' and 'E'.
Observing
The code below works with a single regular expression that is actually composed of two [separated by pipe (|)], so it is possible to build a more granular version, with a regex for each template, as exposed shortly afterwards.
import re
s1 = "The Office [01.06] The Fight.srt"
s2 = 'The.Office.US.S01E06.The.Dundies.720p.srt'
padrao = '(?P<Colchete>\\d{2})\\.(?P<Ponto>\\d{2})|(?P<S>\\d{2})E(?P<E>\\d{2})'
re1 = re.compile(padrao)
print ('## Resultado para s1 ##')
print ('Groups: ',re1.search(s1).groups())
print ('Colchete: ', re1.search(s1).group('Colchete'))
print ('Ponto: ', re1.search(s1).group('Ponto'),'\n')
## Resultado para s1 ##
Groups: ('01', '06', None, None)
Colchete: 01
Ponto: 06
print ('## Resultado para s2 ##')
print ('Groups: ',re1.search(s2).groups())
print ('S: ', re1.search(s2).group('S'))
print ('E: ', re1.search(s2).group('E'))
## Resultado para s2 ##
Groups: (None, None, '01', '06')
S: 01
E: 06
You can even make a more granular version by breaking the regex into 2 and working separately with the templates, something like this:
padrao1 = '(?P<Colchete>\\d{2})\\.(?P<Ponto>\\d{2})'
padrao2 = '(?P<S>\\d{2})E(?P<E>\\d{2})'
re_p1 = re.compile(padrao1)
re_p2 = re.compile(padrao2)
print ('## Resultados para a versão Granular ##')
print ('## Para s1 ##')
print ('Groups: ',re_p1.search(s1).groups())
print ('Colchete: ', re_p1.search(s1).group('Colchete'))
print ('Ponto: ', re_p1.search(s1).group('Ponto'),'\n')
## Resultados para a versão Granular ##
## Para s1 ##
Groups: ('01', '06')
Colchete: 01
Ponto: 06
print ('## Para s2 ##')
print ('Groups: ',re_p2.search(s2).groups())
print ('S: ', re_p2.search(s2).group('S'))
print ('E: ', re_p2.search(s2).group('E'))
## Para s2 ##
Groups: ('01', '06')
S: 01
E: 06
DEMO
This solution would only work for the second string, I need something that works for both. I know that with a little gambiarra you can use this way. But I need to avoid to the maximum
– José Henrique Luckmann
Yeah, after I saw the other template, I edited and I totally changed the version, see that now works for qq string, you will be able to remove all numbers, then just adapt.
– Sidon
\d+
can create some trouble if the series name has numbers. This solution would not meet The 4400– Jefferson Quesado
There you have to have a pattern, you can put the
\d+
between two characters that the template informs, as I said just adapt. Now.... if it comes outside the template pattern, it is paratically impossible– Sidon
@Sidon
r'^.*( \[([0-9]+)\.([0-9]+)\] |\.S([0-9]+)E([0-9]+)\.).*\.srt$'
, taking for an answerSE=\2\4
a Season the third or fourth rear-view mirror andEP=\3\5
the episode the third or fifth rear-view mirror; as the rear-view mirrors 2,3 happen simultaneously and 4,5 also, and that 2,3 is excluding with 4,5 , we have that this expression in a way has more scope and would give the same positive results ;-) Ok, this is a solutionsed
-ica, not pythonica, but is not so absurdly far from being pythonizável– Jefferson Quesado
I found a very pythonic solution, "I stole" from Django, I will edit the answer.
– Sidon
@Sidon marks me when he edits?
– Jefferson Quesado
The first solution adapts more to my problem, I will have to make some adjustments. But it will probably be something in this line.
– José Henrique Luckmann
I would just like to remind you that the templates are not fixed, the user who will inform, and what will define the position of the values are the tags {SE} and {EP}
– José Henrique Luckmann
Note that if you come in the pattern started with 2 digits, the sign E in the middle and then 2 more digits, the regex will always find, no matter what position you are in. The second solution is more sophisticated, if it comes in the default, it will probably never fail. @Jeffersonquesado.
– Sidon
In time: adapting to the first template using the second solution is quite simple.
– Sidon
@Jeffersonquesado, I edited the reply again, with a version that meets the two templates.
– Sidon