$text = <<'EOT'; Message-ID: From: "GenericOnline Pharmacy" To: "Angie Morestead" Subject: Buy drugs online now! Date: Thu, 20 Jan 2011 18:40:18 +0200 Content-Type: multipart/related; boundary="----=_Weigard_drugs_CG_0" EOT $text =~ /^Subject:.+drugs/m; # Anchor just after \n, before Subject. # Matches 'Subject: Buy drugs' $text =~ /\nSubject:.+drugs/; # Equivalent $text =~ /^Subject:.+drugs/ms; # '.' matches newlines, all the way to # '..._Weigard_drugs', which is not what we wanted. $text =~ /^Subject:.+?drugs/ms; # '.' matches newlines, but searches from current string # position, stopping when it matches 'Subject: Buy drugs'. # This is a little slower than the first two, but # equivalent. /s is countered by the .+?, but if 'drugs' # was not in the Subject line, the regex would keep keep # on going. # Here are some fun ones. # The email address should be "Furry Marmot" , or just # marmot@furrytorium.com. Anything else is spam. print "Spam!!!\n" if $text =~ /^(?:From|To):\s*"(?!.+Furry Marmot)[^"]*" /m; # Regarding the [^"]*, if the regex finds Furry Marmot in quotes, it fails and this isn't # spam. But if it finds something else, we still have to match something between the # quotes, and then match the email to determine if it is spam. # I should never see anything from me, to me. print "Spam!!!\n" if $text =~ /(?=^From:[^\n]+marmot\@furrytorium\.com).+^To:[^\n]+marmot\@furrytorium\.com/ms; # This starts at the beginning of header block, finds From: line with my email address, # resets to start of block (because of zero-width lookahead assertion), then finds To: # line with my email address. It is the equivalent of... if ($text =~ /^From:.+marmot\@furrytorium\.com)/m && /^To:.+marmot\@furrytorium\.com/m) { print "Spam!!!\n" } # ...but I can include the single pattern in a list of patterns that I might want to match # against the string.