in reply to Re: Back reference in s///g ?
in thread Back reference in s///g ?

Look-ahead and look-behind regex sub-expressions can be very useful; you should definitely look into them. See perlre and perlretut.

Technically speaking, you are not using back references (which look like \1 or \2), but capture variables (i.e., $1 and $2). A back reference is used within a regular expression to refer to a sub-string that has been captured by the corresponding preceding pair of capturing parentheses. A capture variable is used after a regular expressison has done its work, but also allows access to text captured by the corresponding capturing parentheses. (The replacement string in a s/pattern/replacement/ substitution operation is formed after the regular expression has executed.) And yes, capturing parentheses, like most other good things in life, have a (performance) price, although usually a trivial one.

By the way, it would be possible to do what you want to do (as far as I understand it) without look-ahead/behind regex expressions, as follows:

$in =~ s{ ([^a-zA-Z]) \s+ ([^a-zA-Z]) }{$1\n$2}xmsg;

(that is, any whitespace substring with non-alphabetic characters both before and after it is replaced with a single newline). (However, there is a potential edge-case 'failure' at the very beginning or the very end of the string with this approach. You should consider what you want to have happen if the string either begins or ends with a string of whitespace characters.)