in reply to Simple Regex drives me insane

How about:

/ ^ \# (.*) # Something starting with a hash. | # Or .* # Anything else. /x;

Replies are listed 'Best First'.
Re^2: Simple Regex drives me insane
by ikegami (Patriarch) on Mar 30, 2024 at 18:06 UTC

    It doesn't set $1 to the foo when matching against #foo bli bla, despite an example stating it should.

    It doesn't set $1 to the empty string when matching against bli bla, despite an example stating it should.

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

    For the last two, it doesn't match and leaves $1 unchanged.


    Update: After the update to the parent, the following holds:

    It doesn't set $1 to the foo when matching against #foo bli bla, despite an example stating it should.

    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.

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

      I updated my last reply

      But I think it might be better to step back and consider what you are trying to achieve.

      If you can use code, that may be simpler:

      my $in = "foo"; my $out = $in =~ /#(.*)/ ? $1 : "";

        I'm not the OP.

        The .* should be \S*, \S+, \w*, \w+ or something. It's unclear what should be used exactly, but .* matches too much.

        The above also applies for the first .* in your edited code.

        The second .* in your edited code is useless. You could just leave it out.