Perl Programming Page 8 - String Processing with Perl |
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 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. This replaces "susan" in the variable $flag with "JANE" using the "s" operator. Here is a simple example that validates an email address: 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. Here's an example session: You can have Perl return the position of the last match in a string with the pos() function, and automatically quote special characters with backslashes with the quotemeta() function. 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.html The 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!
blog comments powered by Disqus |