Minutes of perl

Posted on July 29, 2008. Filed under: Perl |

  • Perl Regular Expressions

Simple String Comparisons

The most basic string comparison is

$string =~ m/sought_text/;

The above returns true if string $string contains substring “sought_text”, false otherwise. If you want only those strings where the sought text appears at the very beginning, you could write the following:

$string =~ m/^sought_text/;

Similarly, the $ operator indicates “end of string”. If you wanted to find out if the sought text was the very last text in the string, you could write this:

$string =~ m/sought_text$/;

Now, if you want the comparison to be true only if $string contains the sought text and nothing but the sought text, simply do this:

$string =~ m/^sought_text$/;

Now what if you want the comparison to be case insensitive? All you do is add the letter i after the ending delimiter:

$string =~ m/^sought_text$/i;

Using Simple “Wildcards” and “Repetitions”

Calling these “wildcards” may actually conflict with the theoretical grammar and syntax of Perl, but in fact is the most intuitive way to think of it, and will not lead to any coding mistakes.

.   Match any character
\w  Match "word" character (alphanumeric plus "_")
\W  Match non-word character
\s  Match whitespace character
\S  Match non-whitespace character
\d  Match digit character
\D  Match non-digit character
\t  Match tab
\n  Match newline
\r  Match return
\f  Match formfeed
\a  Match alarm (bell, beep, etc)
\e  Match escape
21  Match octal char ( in this case 21 octal)
\xf0  Match hex char ( in this case f0 hexidecimal)

You can follow any character, wildcard, or series of characters and/or wildcard with a repetiton. Here’s where you start getting some power:

*      Match 0 or more times
+      Match 1 or more times
?      Match 1 or 0 times
{n}    Match exactly n times
{n,}   Match at least n times
{n,m}  Match at least n but not more than m times

Now for some examples:

$string =~ m/\s*rem/i;   #true if the first printable text is rem or REM
$string =~ m/^\S{1,8}\.\S{0,3}/;   # check for DOS 8.3 filename 
                                   #  (note a few illegals can sneak thru)

http://www.troubleshooters.com/codecorn/littperl/perlreg.htm#op_=%7E

 

  • Command Line  Options:

Getopt::Std and Getopt::Long

  use Getopt::Long;

  my $data   = “file.dat”;

  my $length = 24;

  my $verbose;

  $result = GetOptions (“length=i” => \$length,    # numeric

                        “file=s”   => \$data,      # string

“verbose”  => \$verbose);  # flag

http://perldoc.perl.org/Getopt/Long.html

http://perldoc.perl.org/Getopt/Std.html

http://www.wellho.net/forum/Perl-Programming/subset-a-list.html

Advertisements

Make a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Liked it here?
Why not try sites on the blogroll...

%d bloggers like this: