in reply to Re^4: reg expression question
in thread reg expression question

You are right, when used in a regex without an /m modifier, ^ means “match the beginning of the string,” and is equivalent to \A. “Match at the beginning of the line” (which is the definition given in perlre#Regular-Expressions) is strictly correct only when ^ is used in a regex with an /m modifier. So maybe this part of the documentation should be re-worded?

I haven’t used Perl::Critic, and as to this particular policy — well, I’m suspicious of guidelines that say “always do X” regardless of context. In the present case, adding an /m modifier to the regex would, IMO, be misleading, as it would imply (or at least suggest) that the string being matched is expected to contain multiple newlines. But I acknowledge that this is a judgment call, and YMMV.

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Replies are listed 'Best First'.
Re^6: reg expression question
by AnomalousMonk (Archbishop) on Jan 29, 2015 at 11:14 UTC

    It is this "Without the /whatever modifier, whatever means ...; with the modifier it means..." locution that is the rationale for the kneejerk practice of always using the /xms constellation of modifiers with every regex regardless of whether  . ^ $ appear in the regex or not, and regardless of the fact that /x arguably makes it a bit more messy to deal with whitespace.

    I have adopted this practice, first encountered in the (in)famous PBP of TheDamian, because for me, regexes are hard enough without the further confusion of unnecessary degrees of freedom. Now, what does  . (dot) match? Everything! YBPMV.

    Update: See also re '/flags' mode, but I tend to avoid this in PM examples because it only appeared with Perl version 5.14 and has too narrow familiarity.


    Give a man a fish:  <%-(-(-(-<

      Can you please confirm based on your comments I made the following changes
      sub rewrite { my ($string) = @_; my ($left, $right) = split /\s+/, $string, 2; if (my @m = $right =~ /^\d{2}(\d{2})(.*)/) { $right = $m[0] . ($m[1] =~ /^-/ ? '' : '-') . $m[1]; // here are the new additions $right =~ s/\s+//g; $right =~ s/-$//; } else { $right = '-' . $right unless $right =~ /^-/; } return $left . $right; } Please confirm that this is what you meant. Thanks

        Almost; except that PUN VALEY B still isn’t handled correctly. That’s because it doesn’t match the regex, so the if clause isn’t entered and the substitutions are not applied. But if you move the two substitutions from within the if clause to after the else clause — that is, to immediately before the return line — then all the tests pass.

        Hope that helps,

        Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,