XML, the Perl Way

Processing XML with Perl Michel Rodriguez

Control Structures

Perl Idioms

Regular Expressions

Pattern Matching

  my $string= "toto is French for foo";
  if( $string=~ /toto/) { print "toto is in the string\n"; }
  if( $string=~ m{(\w+) is French for (\w+)})
    { print "$1 = $2\n"; }

characters: . is any character, [Aa-z0-9] a character class, \w a word character, \W a non-word character (anything else than a word character), \d a digit, \s a space ([ \t\r\n\f]), ^ is the beginning of a string and $ is the end.

modifiers: ? optional, + repeatable, * optional repeatable, | or

storing a match: the first expression in () is in $1, the second one in $2...

regexp modifiers: i ignore case, g keep the state

while( $string=~ /(\w+)/g { print "$1\n"; }

Pattern Substitutions

  my $string= "toto is French for foo"
  $string=~ s/toto/tata/;
  $string=~ s{foo}{bar};
  $string=~ s{(\w+) is French for (\w+)}{$2 is English for $1};


Control Structures

Perl Idioms