Using backreferences
First a PHP example.
$string = "Hello to the World";
$string = eregi_replace("^.*(to) (the) World.*$","\\1 and \\2");
print($string); #> outputs "to and the"
In Perl, you use the =~ operator to start a RegExp. Below I have used it to get back references which can be used outside of the expression via in this case $1 and $2.
my $string = "Hello to the World"; $string =~ /^.*(to) (the) World.*$/; print $string . " && the last two back references are |$1| and |$2|";
You can use the back references inside the string like below which is like the first PHP example.
my $string = "Hello to the World"; $string =~ s/^.*(to) (the).*$/$1 and $2/gi;
For more information, see in this document for Regular Expressions.
[Click to add or edit comments])
Please prepend comments below including a date