Archive for July, 2008

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)


  • 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

Read Full Post | Make a Comment ( None so far )

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