Home > front end >  multi regex to grab numbers in python
multi regex to grab numbers in python

Time:11-03

I'm not expert with regex but I did this from scratch I need to grab just numbers and letters [a-e] based on this :

SnnEnn   where n represent numbers  
SnnEnnl  where l = letter [a-e]  

nnXnn    where n represent numbers 
nnXnnl   where l = letter [a-e] 

but it fails in few case like this:

02x01      match '02x01'                                      fail (It should be '02' '01')
03x02a     match '03x02'..............groups 'a'             fail (It should be '03' '02' 'a')
S03E01     match 'S03E01'.............groups '03'  '01'      ok
S03E01a    match 'S03E01a'............groups 'S03E01'  'a'   fail.(It should be '03' '01' 'a')

03x02xxxx  match '03X02xxxx' groups '03x02xxxx'    fail (it should be just [a-e] and limit to one letter) 
S03E01xxx  match '03E01xxxx' groups '03e01xxxx'    fail (idem)

regex101

I'm using this regex :

(\d [x]\d \w)([a-e])|(\d [x]\w )|\bS(?P<Season>\d )E(?P<Episode>\d )|(\b[s]\d [e]\d )([a-e])

Thank for you for any help

CodePudding user response:

You might use

\b(S)?(?P<Season>\d )(?(1)E|x)(?P<Episode>\d )(?P<Letter>[a-e]?)\b
  • \b A word boundary
  • (S)? Optionally capture S in group 1
  • (?P<Season>\d ) Group Season - match 1 digits
  • (? Conditional (See this page about conditionals)
    • (1)E If group 1 exists, Match E
    • | Else
    • x Match x
  • ) Close conditional
  • (?P<Episode>\d ) Group Episode - match 1 digits
  • (?P<Letter>[a-e]?) Group Letter - match a range a-e
  • \b A word boundary

Regex demo

  • Related