#!/usr/bin/perl
use strict;
use warnings;
my $s = "sad day
Good day
May be Bad Day
";
$s =~ s/\w+ \w+/_/gm;
print $s;
I am trying to substitute all spaces between words with _, but it is not working. What is wrong with that?
The substitution replaces an entire word (\w+) then a space then an other word by an underscore.
There is really no need to replace (or capture for what matters) those words
$a=~s/\b +\b/_/gm;
will replace a word break ( \b, a zero-width transition between a word and a non word) followed by one or more spaces followed by an other word break, by an underscore. Using \b ensures that you don't replace a space after or before a new line.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With