I’ve got a regular expression with capture groups that matches what I want in a broader context. I then take capture group $1 and use it for my needs. That’s easy.
But how to use capture groups with s/// when I just want to replace the content of $1, not the entire regex, with my replacement?
For instance, if I do:
$str =~ s/prefix (something) suffix/42/
prefix and suffix are removed. Instead, I would like something to be replaced by 42, while keeping prefix and suffix intact.
If you only need to replace one capture then using
@LAST_MATCH_STARTand@LAST_MATCH_END(withuse English; seeperldoc perlvar) together withsubstrmight be a viable choice: