in reply to Re^2: regex in REPLACEMENT in s///
in thread regex in REPLACEMENT in s///

you only need parentheses when there's a capture

Are they not also needed where there is optionality?

Could this be (sensibly) rewritten without the parenthesis?

$example =~ s/hiss(es)?/leak/;
To substitute hiss/hisses for leak but not substitute hisse or hisss.

Replies are listed 'Best First'.
Re^4: regex in REPLACEMENT in s///
by haukex (Archbishop) on Sep 13, 2023 at 06:37 UTC
    Could this be (sensibly) rewritten without the parenthesis? $example =~ s/hiss(es)?/leak/;

    Although tybalt89 probably can think of some ways, my answer would be: No, I would keep the parentheses in that case. However, they can be made non-capturing by writing /hiss(?:es)?/ (or with the /n modifier, new since 5.22). I think choroba's point was that in s/(\d)/3/g, the parens serve no purpose at all.

      Although tybalt89 probably can think of some ways, my answer would be: No

      I am sure that tybalt89 and other wizards in The Monastery could contrive something that would have the same functionality...that's why I added "sensibly" to my question!

      Thanks for clarifying - I thought that was the case but I am far from being a regexp wizard so I wanted to check. Now I need to go and look up the /n modifier as that is new to me. I need to look up others as well as the only ones I ever use are /igc I think although I feel there is at least one more I use occasionally!