Perl: regular expression explanation

The tutor follows up on his post about the variable password checker.

In my Nov 18 post I show a short script that checks a three-digit entry for a match with the pattern m[any character]m. The entry m4m is a match, for example. However, m4mi is not a match (too long). Neither is tom; it doesn’t follow m[character]m.

The code that can distinguish the pattern

m[any character]m

is

if ($word=~/^n.n$/){
print “correct”;
}

The =~ means “contains match”. The period in the middle means “any single character”. The ^ means the match must occur at the beginning of $word, while the $ means it must occur at the end. Therefore, the string can only be three characters long.

HTH:)

Source:

McGrath, Mike. Perl in easy steps. Southam: Computer Step, 2004.

Robert’s Perl tutorial

Jack of Oracle Tutoring by Jack and Diane, Campbell River, BC.

Tagged with: , ,

Leave a Reply