Services & Resources / Wolfram Forums / MathGroup Archive
-----

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: [mg97231] Re: When a string does not match itself
  • From: "Sjoerd C. de Vries" <sjoerd.c.devries at gmail.com>
  • Date: Mon, 9 Mar 2009 01:01:52 -0500 (EST)
  • References: <got8h2$go0$1@smc.vnet.net>

Hi Ingolf,

My explanation would be the following

The first "\\*" is a standard string. In such a string the '\' is an
escape character. For instance, "\n" is the newline character (or
perhaps the lf+cr, I forgot). It's a single character, with a single
character code. To be able to input the '\' in a string as a literal
character it can be escaped by itself. So, "\\" is the character '\'.
Therefore, "\\*" is the concatenation of the characters '\' and '*'.

The second argument of StringMatchQ can be a substring or a string
pattern. The latter may be present in an abbreviated form. In this
form, "*" stands for zero or more characters. If you want to match
with a literal '*', you'd have a problem as "\*" would be the escaped
character '*', which is a single character with character code 63432
(as can be checked with ToCharacterCode["\*"]).

So, in an abbreviated pattern string '*' has to be escaped twice,
before the pattern is interpreted as '*', i.e. as "\\*". This is
documented in the "More Information" section of StringMatchQ's
documentation.

Therefore, StringMatchQ["\\*","\\*"] compares the literal characters
'\' + '*' with the literal '*', which of course returns False.

StringCases and StringPosition don't have the abbreviated string
pattern option. The can only use substrings, StringExpression and
RegularExpression. That's why they interpret the second "\
\*"differently.

To have StringMatchQ match the two strings you could use Verbatim:

In[112]:= StringMatchQ["\\*", Verbatim["\\*"]]

Out[112]= True

Cheers -- Sjoerd

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? 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: Mathematica 7.0.1.0 and some General Comments
  • Next by Date: Re: Mathematica 7.0.1.0 and some General Comments
  • Previous by thread: Re: When a string does not match itself
  • Next by thread: Quantum Mathematica sneak preview of commutator and operator algebra in Mathematica