Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Matching strings with non-word characters

by chas (Priest)
on Apr 17, 2006 at 13:14 UTC ( #543808=perlquestion: print w/replies, xml ) Need Help??
chas has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to match strings such as "X \ n a x" which appear in spam email so I can filter. I can do this but not in a completely satisfactory way. I guess I've just gotten myself confused, but I don't understand why
$_="X "; if(m'X\ '){print "OK1\n";} $_='X\ '; if(m'X\ '){print "OK2\n";}
produces "OK1" only. I would expect the first match to fail and the second to succeed. Thanks for any comments!

Replies are listed 'Best First'.
Re: Matching strings with non-word characters
by Tanktalus (Canon) on Apr 17, 2006 at 13:19 UTC

    That's because \ is a special character. Thus, you need to escape it if you want it to appear literally. To escape it, just use a backslash - thus, you get m'X\\ '. Although I think the single-quote as a seperator is a bit wierd.

      Thanks! I used single quote as separator because I thought that would make the "\" not considered special. I guess that is wrong. (Using single quote prevents interpolation in patterns, though.)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://543808]
Approved by Tanktalus
[Discipulus]: oh well we cant afford two monks fired in a day!
[LanX]: any news from tye ' s search?
[MidLifeXis]: Ugh - who else?
[Discipulus]: so the right term is 'gired'
[Discipulus]: look at ot by karl
Discipulus gired: adj the work position that stands between hired and fired..
LanX recommends inner immigration, work less hard and enjoy pay roll
[MidLifeXis]: ahh, yeah, didn't look through nodes yet today.
[erix]: haha
[MidLifeXis]: Ahh well, time to get back to it, so there isn't a reason to take that choice out of my hands :-)

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (14)
As of 2017-03-23 12:46 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (286 votes). Check out past polls.