The m Operator


Introduction

The m operator is not difficult to use, but it is important to test one's understanding of it as one progresses to more and more complicated examples, hence this page. The intention is that as one reads it, one exercises using the lower two frames, so that experiments (hacking) will lead to expertize. A word of warning, the behavior of this page will sometimes differ slightly from what is expected in a naked Perl program, since this page passes information back and forth through the World Wide Web, and there are restrictions inherent in the WWW technology which limits us. I apologize ahead of time if there are some strings which give inappropriate results, and I ask you to please send me examples using this mailer so that I can see if the current implementation can be improved (thanks in advance!).
The normal use of the m operator is in a construct such as:
(m/pattern/){do things;do other things}
and
&call_subroutine if /pattern/;
Thus, the m operator returns a logical (true/false) value, where true is '1' and false is ' ' here. (Thanks to Steve Palincsar for suggesting this presentation.)
In both of the above examples, the searchs a string for the occurrence of another string , and returns a 'true' (here shown as '1') if the search pattern is found in the searched string, and a 'false' (here shown as a blank string) otherwise. The searched string is usually in the special Perl variable $_ (unless bound to some other scalar using the =~ operator). before the search is called.
if ( $searched_string  =~/ pattern /) {do things in this block;}
or
&call_subroutine if  $searched_string  =~ / pattern /;


Simple Discovery Patterns

To exercise this page, and see the effect of the m-operator, enter "Your Name" (without the quotation marks) in the $instring box below, and enter "ou" (without the quotation marks) in the search pattern box, and press Submit. You should see a returned value of 1 for $outstring
Now, change the search pattern to "yo" and submit again. You should see a returned value of blank for $outstring And now, click on the ignore button, and re-submit.

Not So Simple Discovery Patterns

Try some of the following, with and without the ignore case (note, the global doesn't work in this environment. Repeated use of the m command with global extends the search from the last match onwards. Notice that the `pos' command is included here if global is called, to show where the next m-command would start from.):
searched stringsearch patternExplanation
How are you?howareyouno match possible
How are you?how.are.youThe period matches any and all characters (ignore required)
How are you?how..re.YOUThe period matches any and all characters (ignore required)
How are you?[aeiou]matches any vowel
bcdfghjklmnpqrstvwxyz[aeiou]matches any vowel
aeiou[^aeiou]matches any non vowel
3.14159[0-9]matches any numeric between 0 and 9 inclusive
3.14159[^0-9]matches any non-numeric (the period!)
+3.14159\+matches any (escaped) plus sign


It is time to learn about the escape character, the backwards slash. In the last example, we wanted to find a plus sign, but it turns out that a plus sign is a special character which Perl uses in regular expressions (later, see below). So we can not use it naked (as it were), but must make sure that it is not pre-interpreted by Perl. Hence the backward slash (\) which means that the next character is to be used literally, i.e. not interpreted. If one wanted to search for two plus signs:
searched stringsearch patternExplanation
++3.14159\+\+matches any (escaped) juxtaposed plus signs
+3.14159+\+\+fails
+3.14159+\+.......\+succeeds

Searching for Words If one wants to locate discrete words, Regular Expressions has a way. use \bword\b to do the job.
searched stringsearch patternExplanation
this is a cat and this is a dog\bcat\bmatches a cat as an isolated word
this is a scat and this is a dog\bcat\bfails to match a cat as it is not isolated
\b is called an anchor (there are others). Technically, it is a zero-width anchor, consuming no characters in and of itself.

Searching for Embedded Characters If one wants to locate discrete characters buried inside other characters, Regular Expressions has a way. use \w+char\w+ to do the job.
searched stringsearch patternExplanation
catordog\wor\wmatches the the characters `or'
catordog\w+or\w+matches the "or" also
cat or dog\w+or\w+does not match the characters "or", since \w calls for a alphanumeric character (and/or underscore) and the `+' says, match one or more.
cat or dog\sor\smatches the equal sign also
\w matches a single alphanumeric character (A..Z,a..z, and underscore = _). The `+' sign appended to \w, i.e., \w+, matches one or more alphanumeric characters. \s matches what is known as whitespace.

Searching for Characters at the Front or End of a String If one wants to locate characters at the beginning (`^') or the end (`$`) of a string, Regular Expressions has a way. Use ^ chars and $ chars (beware of the order) to do the jobs.

searched stringsearch patternExplanation
catordog^cmatches the `c' at the beginning
catordog^adoesn't match the `a' as it is not at the beginning
cat or dogg$matches the ending `g'.
cat or dogog$matches the ending `og'.
cat or dogo$doesn't match the ending `o' as `o' is not at the end.
cat or dogo.$matches the ending `og' as well as ox, oy, o-anything (at the end).
Footnote: The global variable sets up a memory option, so that each time the m-operator is called, it finds the next occurence of the search pattern in the string. We have implemented that here to be that after the m(atch) has been done, the pos value is printed, and th m(atch) is done again, continuing on until there is no m(atch). We hope this is appropriate, but criticisms are both appreciated and accepted!
Back to Table of Contents
Forward to More Complicated m expressions.