Perl
  Home arrow Perl arrow Page 8 - String Processing with Perl
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PERL

String Processing with Perl
By: Harish Kamath, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 62
    2003-04-16


    Table of Contents:
  • String Processing with Perl
  • Jumping Jacks
  • Choppy Waters
  • Making New Friends
  • Not My Type
  • Of Jumping Cows And Purple Pumpkins
  • On The Case
  • Desperately Seeking Susan

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    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.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!

     
     
    >>> More Perl Articles          >>> More By Harish Kamath, (c) Melonfire
     

       

    PERL ARTICLES

    - Basic Charting with Perl
    - Using Getopt::Long: More Command Line Option...
    - Command Line Options in Perl: Using Getopt::...
    - Web Access with LWP
    - More Templating Tools for Perl
    - Site Layout with Perl Templating Tools
    - Build a Perl RSS Aggregator with Templating ...
    - Looping, Security, and Templating Tools
    - Perl: Bon Voyage Lists and Hashes
    - Templating Tools
    - Perl: Number Crunching
    - Perl Debuggers in Detail
    - Debugging Perl
    - Perl: More on Lists and Hashes
    - Perl: Dimensional Lists





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
    Stay green...Green IT