Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Programming
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Databases
Mail Systems
openSolaris
Eclipse Documentation
Techotopia.com
Virtuatopia.com
Answertopia.com

How To Guides
Virtualization
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Windows
Problem Solutions
Privacy Policy

  




 

 

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

6.3 Pattern Matching

Now that you are familiar with some of the basics of regular expressions, you probably want to know how to use them in Perl. Doing so is very easy. There is an operator, =~, that you can use to match a regular expression against scalar variables. Regular expressions in Perl are placed between two forward slashes (i.e., //). The whole $scalar =~ // expression will evaluate to 1 if a match occurs, and undef if it does not.

Consider the following code sample:

use strict; while ( defined($currentLine = <STDIN>) ) { if ($currentLine =~ /^(J|R)MS speaks:/) { print $currentLine; } }

This code will go through each line of the input, and print only those lines that start with "JMS speaks:" or "RMS speaks:".




 
 
  Published under the terms of the GNU General Public License Design by Interspire