Re: When a string does not match itself
- To: mathgroup at smc.vnet.net
- Subject: [mg97202] Re: [mg97198] When a string does not match itself
- From: Bob Hanlon <hanlonr at cox.net>
- Date: Sun, 8 Mar 2009 05:49:38 -0500 (EST)
- Reply-to: hanlonr at cox.net
"\" has a special meaning in strings.
\n is new line, \t is tab, and otherwise \ is a literal directive
Print["This is \ttab and \
newline\nliteral quote\" and literal \
backslash\\ or \\\nat the end of a line"]
This is tab and newline
literal quote" and literal backslash\ or \
at the end of a line
And * is a wildcard in strings
StringMatchQ[#, "mon*ey"] & /@
{"money", "monkey", "monk's key"}
{True,True,True}
Except when it is made literal
StringMatchQ[#, "mon\*ey"] & /@
{"money", "monkey", "monk's key"}
{False,False,False}
StringMatchQ["\\*", "\\*"]
False
I suspect that because of the multiple special characters and how and when that they are internally evaluated, that this is comparing different expressions than what you intended.
StringMatchQ["\\", "\\"]
True
StringMatchQ["\*", "\*"]
True
StringMatchQ["\"", "\""]
True
Bob Hanlon
---- ingolf.dahl at telia.com wrote:
=============
This is a maybe a trivial question, but I have not found the explanation in
help (I am using Mathematica 7.0.0):
Normally a string matches itself:
In[270]:= StringMatchQ["monkey", "monkey"]
Out[270]= True
But
In[271]:= StringMatchQ["\\*", "\\*"]
Out[271]= False
Why? Compare also to
In[4]:= StringCases["\\*", "\\*"]
Out[4]= {"\\*"}
In[267]:= StringPosition["\\*", "\\*"]
Out[267]= {{1, 2}}
Best regards
Ingolf Dahl
Sweden
ingolf.dahl at telia.com