[Python] Match word "A". If "B" is next word, match too

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

  1. Stack

    Stack Membro Participativo

    Using regex in Python I want to get the word "net" from string and if the word "wt" is the next, get too

    Example:

    string = "12 boxes net wt 1.oz 3.6g" #Must returns "net wt"

    string = "12 boxes net 1.oz 3.6g" #Must returns "net"

    Between "net" and "wt" could be "." "," or spaces

    I used this regex but didn't work on the second example

    pat = r'net([,.\s]*?)(wt?)'

    Continue reading...

Compartilhe esta Página