in reply to Simple Regex drives me insane

Maybe /(?:#(\w+))?/ assuming \w+ is enough to capture "the string"?

Replies are listed 'Best First'.
Re^2: Simple Regex drives me insane
by cheerydog (Initiate) on Mar 30, 2024 at 15:13 UTC
    Thank you very much for you answer. It works but there is a problem: I would to successfully match the string if '#foo' is not there (0 or 1 occurences)

      It already does do that.

      Specifically, it always matches, setting $1 to foo if #\w+ is found at the start of the string, and setting $1 to undef otherwise.

        That said, I believe it has other problems.

        It doesn't set $1 to foo when matching against bli #foo bla.

        It doesn't set $1 to the empty string when matching against bli bla. However, it does set it to undef, which might be close enough.