regex - Replace pattern with one space per character in Perl -
let's i'm trying match url regular expressions:
$text = 'http://www.google.com/'; $text =~ /\bhttp:\/\/([^\/]+)/; print $1; # prints www.google.com i replace pattern matches 1 space each character in it. instance, considering example above, end text:
# http:// / is there simple way this? finding out how many characters matched pattern has , replacing same number of different characters?
thank you.
one simple way is:
$text =~ s!\b(http://)([^/]+)!$1 . " " x length($2)!e; the regexp \b(http://)([^/]+) matches word boundary, literal string http://, , 1 or more non-slash characters, capturing http:// in $1 , non-slash characters in $2. (note i've used ! regexp delimiter above instead of usual / avoid leaning toothpick syndrome.)
the e switch @ end of s/// operator causes substitution $1 . " " x length($2) evaluated perl code instead of being interpreted string. evaluates $1 followed many spaces there letters in $2.
Comments
Post a Comment