MathGroup Archive 2009

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: When a string does not match itself

  • To: mathgroup at smc.vnet.net
  • Subject: [mg97228] Re: When a string does not match itself
  • From: Szabolcs <szhorvat at gmail.com>
  • Date: Sun, 8 Mar 2009 05:54:36 -0500 (EST)
  • References: <got8h2$go0$1@smc.vnet.net>

On Mar 7, 9:42 am, <ingolf.d... 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?

Yes, this is a bit tricky.  In a pattern, * stands for "any sequence
of characters".  So we need a way to construct a pattern that only
matches the * character.  This is done by escaping, i.e. preceding the
* by a backslash.  To make things more complicated, we also need to
escape the backslash to include it into a Mathematica string.

So the patter "\\*" is just an escaped *, i.e. it matches a "*", and
"*" only.  To match "\\*", we need to use "\\\\*".

In[3]:= StringMatchQ["\\*", "\\\\*"]
Out[3]= True

Escaping can get ugly.

> Compare also to
>
> In[4]:= StringCases["\\*", "\\*"]  
>
> Out[4]= {"\\*"}
>
> In[267]:= StringPosition["\\*", "\\*"]  
>
> Out[267]= {{1, 2}}
>
> Best regards
>
> Ingolf Dahl
>
> Sweden
>
> ingolf.d... at telia.com



  • Prev by Date: Re: A newbee to Mathematica
  • Next by Date: Re: Re: Mathematica 7.0.1.0 and some General Comments
  • Previous by thread: Re: When a string does not match itself
  • Next by thread: Re: When a string does not match itself