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

 
Developer Updates  
Free Website Content 
IBM Developerworks
 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: 4 stars4 stars4 stars4 stars4 stars / 49
    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:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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

    Route your faxes to your email inbox. Private, secure fax numbers available from CallWave. Choose your fax number.

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

     

       

    PERL ARTICLES

    - Perl: A Continuing Look at Hashes and Multid...
    - Perl: Another Round with Hashes
    - Perl Hashes
    - Perl Lists: A Final Look at List::Util
    - Perl Lists: Utilizing List::Util
    - Perl Lists: The Split() Function
    - SQL and CGI with Perl and DBI
    - Perl Lists: More Functions and Operators
    - SELECT Queries and Perl
    - Perl Lists: More on Manipulation
    - Creating a Database with Perl and DBI
    - Perl: Sailing the List(less) Seas
    - Perl and DBI
    - Perl: Concatenating Text and More
    - Perl Text: Quoting Without Quote Marks

     
    Accelerating Trading Partner Performance
     
    Competing on Analytics
     
    Cost Effective Scaling with Virtualization and Coyote Point Systems
     
    Five Checkpoints to Implementing IP Telephony
     
    Hosted Email Security: Staying Ahead of New Threats
     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway