[Python] In a Python regex (re), how can I match the start of a string (^) OR a...

Discussão em 'Python' iniciado por Stack, Dezembro 21, 2024.

  1. Stack

    Stack Membro Participativo

    I am using the Python regex package re for example to match a hashtag or url. A hashtag starts with a #, which cannot be in the middle of a word. Hence, it can be at the start of a word, which I can find with \W OR at the start of the string (^). How can I catch both cases, i.e. ^ OR \W? I tried things like (b for my bytes-like object)

    hashtag = rb'[^|\W]#\w*'


    and all kinds of variation trying to escape ^, leaving out |, using \b, etc.

    Another example would be an url, which starts with "http(s)://":

    url = rb'[^|\W]https?:\/\/\w*'


    How can this be done? Note that I am using Python, not Javascript!

    Continue reading...

Compartilhe esta Página