Search and Replace
In Perl you can perform a search and replace just like the eregi and preg functions in PHP. Here is an example.
Basically, in this example, we have a $string and we want to grab the variable values from a query string. You can see that we use =~ to find the patterns. This is equivalent to ereg or preg functions in PHP. You can actually perform an ereg_replace or preg_replace like in PHP. It just stores the back references in the $ variable.
#!/usr/local/bin/perl
$string = "foo_name=DLP&bar_name=RT";
$string =~ m/foo_name=(.+)&bar_name=(.+)/gi;
print("foo_name=$1 and bar_name=$2 \n"); ### outputs: foo_name=DLP and bar_name=RT ###
If you wanted to manipulate a string directly and store the new value back into it's variable, you would do the following. Basically, let's make the $string above have only the value DLP.
#!/usr/local/bin/perl $string = "foo_name=DLP&bar_name=RT"; $string =~ s/foo_name=(.+)&bar_name=.+/$1/gi; print($string . "\n"); ### outputs: DLP ###
[Click to add or edit comments])
Please prepend comments below including a date