in reply to Best Practice: Order of regex modifiers?

Hi Rolf,

Most regexes I write only have a few modifiers, so it's hard to get confused no matter what order they are in, and in those cases I don't see order as a problem. Although I don't have a strong opinion on this, if I did have to settle on a standard, I might consider the order that Perl uses when regexes are stringified, e.g.

$ perl -wMstrict -le 'print qr/abc/msixpoun' (?upmsixn:abc)

Although unfortunately, it seems this order doesn't match with the documentation, qr/STRING/msixpodualn, which is another possible ordering...

Update: Also, I often place those modifiers that change the behavior of the regex, like /gc, first, so they're immediately obvious.

Regards,
-- Hauke D

Replies are listed 'Best First'.
Re^2: Best Practice: Order of regex modifiers?
by LanX (Saint) on Feb 01, 2017 at 15:35 UTC
    Hi Hauke,

    Thanks, I ignored the "natural order" of perldocs ;-)

    > Update: Also, I often place those modifiers that change the behavior of the regex, like /gc, first, so they're immediately obvious.

    Well all modifiers change the behaviour of a regex, don't you think?

    (I think that's why they are called modifiers ;-)

    This leads to my suggestion to order (or at least group) by importance...

    Cheers Rolf
    (addicted to the Perl Programming Language and ☆☆☆☆ :)
    Je suis Charlie!

      Hi Rolf,

      Well all modifiers change the behaviour of a regex, don't you think?

      Well yes, but: some change how the pattern of the regex is treated, like /xmsialud, whereas some change how the regex operator, like m//, behaves. For example, the return values of m// are quite different from those of m//g, and /g doesn't affect how the pattern is treated.

      Regards,
      -- Hauke D

        Hi Hauke,

        I agree that modifiers like /gcr are fundamentally changing the command.

        That's why in other languages like JS one really needs different commands to achieve the same results.

        There is no easy answer how to prioritize the order...

        ...if possible commands called sr/// or mg// would IMHO be the best choice.

        Cheers Rolf
        (addicted to the Perl Programming Language and ☆☆☆☆ :)
        Je suis Charlie!