First the regex that does what you want (as most regexes assuming a lot): $line =~ s/\s+(\S+)\s+/:$1:/;
which will replace the first instance of "a series of whitespace followed by a series non-whitespace followed by a series of whitespace", with a "colon, followed by the set of non-whitespace matched, followed by another colon".You will have to modify this further to get the result you posted as it is also removing the set of spaces after "three" and replacing it with one space.
Any idea what I'm doing wrong?
Your code can be broken down to a pattern like so:
. will match any character besides \n. The {2} at the end means do this twice. This has to be followed by
\s+ which means the pattern has to have at least one whitespace character but will capture as many of these in a row as it can.
When your pattern matches the first time in the line it will replace it with a colon. As per usual,let me direct you to perlre for your regex needs. -enlil
|