String Processing with Perl - Desperately Seeking Susan (Page 8 of 8 )
You can also search for specific patterns in your strings with regular expressions, something that Perl supports better than most other languages.
In Perl, all interaction with regular expressions takes place via an equality operator, represented by =~
$flag =~ m/susan/
$flag returns true if $flag contains "susan" using the "m" operator.
You can also perform string substitution with regular expressions with the "s" operator, as in the following exanple.
$flag =~ s/susan/JANE/
This replaces "susan" in the variable $flag with "JANE" using the "s" operator.
Here is a simple example that validates an email address:
#!/usr/bin/perl
# get input
print "So what's your email address, anyway?\n";
$email =
;
chomp($email);
# match and display result
if($email =~
/^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-]+)+/)
{
print("Ummmmm....that sounds good!\n");
} else
{
print("Hey - who do you think you're kidding?\n");
}
Obviously, this is simply an illustrative example - if you're planning to use it on your Web site, you need to refine it a bit. You have been warned!
If you want to find out the number of times a particular pattern has been repeated in a string, Perl offers the very cool "tr" operator.
#!/usr/bin/perl
# get input
print "Gimme a string: ";
$string =
;
chomp($string);
# put string into default variable for tr
$_ = $string;
# check string for spaces and print result
$blanks += tr/ / /;
print ("There are $blanks blank characters in \"$string\".");
Here's an example session:
Gimme a string: This is a test.
There are 3 blank characters in "This is a test.".
You can have Perl return the position of the last match in a string with the pos() function,
#!/usr/bin/perl
$string = "The name's Bond, James Bond";
# search for the character d
$string =~ /d/g;
# returns 15
print pos($string);
and automatically quote special characters with backslashes with the quotemeta() function.
#!/usr/bin/perl
$string = "#@!#@!#@!";
$string = quotemeta($string);
# returns \#\@\!\#\@\!\#\@\!
print $string;
Sadly, that's about all we have time for. In case you want more, consider visiting the following links:
The Perl string API, at
http://www.perldoc.com/perl5.6/pod/perlfunc.htmlThe Perl 101 series, at
http://www.devshed.com/c/a/Perl/Perl-101-Part-1--The-Basics/A discussion of regular expressions, at
So What's A $#!%% Regular Expression, Anyway?!Until next time...be good.
Note: Examples are illustrative only, and are not meant for a production environment. Melonfire provides no warranties or support for the source code described in this article. YMMV!
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |