[Python] Use regex to find and merge words in a string Python

Discussão em 'Python' iniciado por Stack, Julho 15, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to find a way to match and merge the teams name from a string like below. I've tried few different ways with regex but was unsuccessful. few examples:

    '30 Detroit Red Wings 12 47:06 3 8 1 3 7 0.292'

    '31 Los Angeles Kings 11 47:45 4 7 0 4 8'

    24 Anaheim Ducks 12 47:49 7 5 0 7 14 0.583

    I want the output to look like this:

    [30, 'Detroit Red Wings', 12, 47:06, 3, 8, 1, 3, 7, 0.292]

    [24, 'Anaheim Ducks', 12, 47:49, 7, 5, 0, 7, 14, 0.583]

    Here is what I tried with regex but with no success:

    pattern = re.compile(r'\b\w+\b')
    matches = pattern.finditer(i)

    Continue reading...

Compartilhe esta Página