user14857172
user14857172

Reputation:

Regex numbers and letters in python

I'm figuring out how get '02' and '05' or any other numbers . I've difficulty when there are letters after numbers ( for example : 'a' and 'b' or any other letters)

title = "Nursing informatics S02E05ab Jack"       ->02 and 05
title = "Medical diagnosis   S06E06ku Peter"      ->06 and 06
title = "medical protection  S01E02bc Katharina"  ->01 and 02

I tried like this , but it always returns 'None'

result = re.search(r"\b(?:e?)?\s*(\d{2,3})(?:[a-z]?)?\b", title, re.IGNORECASE)

It should only get number next S and E. For example, books 2004 must return None.

Thank you all

Upvotes: 2

Views: 146

Answers (2)

Hussain Bohra
Hussain Bohra

Reputation: 1005

A following regex function (findall) can identifies all specified patterns:

import re
s = "Nursing informatics S02E05ab Jack"
re.findall('[0-9]+', s)

Output:

['02', '05']

Upvotes: 4

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627488

You can use

\bS(?P<Season>\d+)E(?P<Episode>\d+)

See the regex demo. Details:

  • \b - a word boundary
  • S - a letter S
  • (?P<Season>\d+) - Group "Season": one or more digits
  • E - a E letter
  • (?P<Episode>\d+) - Group "Episode": one or more digits

See the Python demo:

import re
title = "Nursing informatics S02E05ab Jack" 
m = re.search(r'\bS(?P<Season>\d+)E(?P<Episode>\d+)', title)
if m:
  print( m.groupdict() )
# => {'Season': '02', 'Episode': '05'}

Upvotes: 1

Related Questions