Fulvio Scapin
Fulvio Scapin

Reputation: 129

Perl m// operator inexplicably fails to match a simple regex

Today, while writing a regex to match a prompt output in order to interact with a program through IPC::RUN, I came across an inexplicable failure in matching a simple regex.

$ perl -E 'say "OK" if "mbox [email protected]> " =~ /mbox\s+.*@.*> /s'
$
$ perl -E 'say "OK" if "mbox [email protected]> " =~ /mbox\s+.*@t.*> /s'
OK
$

I thought it could be a problem of interpolation of «@» as a sigil, but

$ perl -E 'say "OK" if "mbox [email protected]> " =~ /mbox\s+.*\@.*> /s'
$

It's so simple I can't seem to understand where the problem may lie.

Perl version is 5.10.1 on an Ubuntu 10.4

Any ideas?

Thanks in advance

Upvotes: 3

Views: 273

Answers (3)

Ed Guiness
Ed Guiness

Reputation: 35277

I think you were on the right track but looking at interpolation in the wrong place.

Consider what happens to the @ in your string rather than your regex.

Upvotes: 7

Michael Küller
Michael Küller

Reputation: 3994

Try this (putting an extra \ before the @ in your string):

$ perl -E 'say "OK" if "mbox user\@testdomain.it> " =~ /mbox\s+.*\@.*> /s'

Upvotes: 3

a'r
a'r

Reputation: 37009

You need to escape the @ in the string to be matched or use single quotes - this is awkward from the shell. Also, you had an extra space at the end of your regex.

perl -E 'say "OK" if "mbox [email protected]> " =~ /mbox\s+.*@.*> /s'  # Yours
perl -E 'say "OK" if "mbox user\@testdomain.it> " =~ /mbox\s+.*@.*>/s'  # Working

Upvotes: 4

Related Questions