Perl Regular Expressions

From Wiki
Revision as of 18:27, 1 February 2011 by Scott (talk | contribs) (Created page with '== Perl Regular Expressions == <source lang="perl"> /a.c/ # (1 of any character) matches "abc" but not "ac" or "abbc" /a*c/ # (0 or more) matches "c", "ac", "aac", …')
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Perl Regular Expressions

/a.c/        # (1 of any character) matches "abc" but not "ac" or "abbc"
/a*c/        # (0 or more) matches "c", "ac", "aac", "aaac", but not "abc"
/a+c/        # (1 or more) matches "ac", "aac", "aaac", but not "abc" or "c"
/a?c/        # (0 or 1) matches "c" and "ac" but not "aac", "aaac" or "abc"
/a{2,3}c/    # (2 to 3) matches "aac" and "aaac" but not "ac"
/a{2,}c/     # (2 or more) matches "aac" and "aaaaaac" but not "ac"
/a{2}c/      # (2) matches "aac"
/a(bc)?d/    # matches "ad" and "abcd"
/a(bb|cc)d/  # matches "abbd" and "accd"
/a[bc]d/     # matches "abd" and "acd"
/a[0-9]+c/   # matches "a123c" but not "ac" or "a1b2c"
/a[^0-9]c/   # matches "abc" but not "ac" or "a1c"
\d matches a digit [0-9]
\w matches a word character [A-Za-z0-9_]
\s matches a whitespace character [\f\t\n\r ]
\D = [^\d], \W = [^\w], \S = [^\s]
/[\dA-Fa-f]+/  # matches hexadecimal numbers
m([\dA-Fa-f]+) # same, can enclose with arbitrary punctuation pairs
m{http://}     # matches "http://"
/yes/i         # (case insensitive) matches "yes", "Yes", and "YES"
/^>/           # match ">" character at beginning of line
/>$/           # match ">" character at end of line
/^abc$/        # only matches "abc"
/^\s*$/        # matches any line containing only whitespace
/\babc\b/      # (word boundary) matches "my abc" but not "abcd"
# (...) saves regular expression submatches to memory
$_ = "Hello there, neighbor";
if (/(\S+) (\S+), (\S+)/) {
  print "words were $1 $2 $3\n";
}
# automatic match variables
# $` is everything before the match
# $& is the match itself
# $' is everthing after the match
if ("Hello there, neighbor" =~ /\s(\w+),/) {
  print "words were $` $& $'\n";
}
my $string = "Here is my string.";
if ($string =~ /[Hh]ere/){...}  # matches
# translate lowercase vowels to upper
$string =~ tr/aeiou/AEIOU/;   # HErE Is my strIng.
# delete all whitespace (g means global)
$string =~ s/\s+//g;          # HErEIsmystrIng.
if ($string =~ /[aeiou]/) {
    print "Found lowercase vowels."
}
# may use non-slash delimiters
s{^https://}{http://}

A Program for Testing Regular Expressions

#!/usr/bin/perl
while (<>) {                   # take one input line at a time
  chomp;
  if (/YOUR_PATTERN_GOES_HERE/) {
    print "Matched: |$`<$&>$'|\n";  # the special match vars
  } else {
    print "No match: |$_|\n";
  }
}

Uses for Regular Expressions

@fields = split /:/, "abc:def:g:h";    # gives ("abc", "def", "g", "h")
@fields = split /\s+/, "I am a cow.";  # gives ("I", "am", "a", "cow.")
@fields = split ' ', "I am a cow.";    # gives ("I", "am", "a", "cow.")
my $sentence = join '_', @fields;      # gives "I_am_a_cow."