When a string does not match itself

11 views
Skip to first unread message

ingol...@telia.com

unread,
Mar 7, 2009, 2:42:26 AM3/7/09
to
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

ingol...@telia.com

Bob Hanlon

unread,
Mar 8, 2009, 6:49:38 AM3/8/09
to
"\" 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

---- ingol...@telia.com wrote:

=============

Bob F

unread,
Mar 8, 2009, 6:51:12 AM3/8/09
to
On Mar 7, 12:42 am, <ingolf.d...@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...@telia.com

The second argument to StringMatchQ[] is a "pattern" and the first
argument is a string, so if you wanted to you could

StringMatchQ["*", "\\*"]

and you would get a value of True back. Look over the documentation of
StringMatchQ[] and "String Patterns" guide and tutorial for more info.

-Bob

Rob Mayoff

unread,
Mar 8, 2009, 6:51:24 AM3/8/09
to
On Mar 7, 1:42 am, <ingolf.d...@telia.com> wrote:
> In[271]:= StringMatchQ["\\*", "\\*"]

ref/StringMatchQ says:

StringMatchQ allows both ordinary StringExpression string patterns,
as well as abbreviated string patterns containing the following
metacharacters:

* zero or more characters
@ one or more characters, excluding uppercase letters
\\*, etc. literal *, etc.


Raffy

unread,
Mar 8, 2009, 6:51:47 AM3/8/09
to
StringMatchQ["\\*", Verbatim["\\*"]]

dbr...@gmail.com

unread,
Mar 8, 2009, 6:53:53 AM3/8/09
to
You have to escape both the \ and the * as in

StringMatchQ["\\\*", "\\\*"]

since both are special characters....


On Mar 7, 2:42 am, <ingolf.d...@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...@telia.com


Szabolcs

unread,
Mar 8, 2009, 6:54:39 AM3/8/09
to
On Mar 7, 9:42 am, <ingolf.d...@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...@telia.com


Sjoerd C. de Vries

unread,
Mar 9, 2009, 2:01:52 AM3/9/09
to
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...@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...@telia.com


Ingolf Dahl

unread,
Mar 9, 2009, 6:17:47 AM3/9/09
to
A lot of thanks to all that have responded.
This was so trivial that I simply MUST put the blame on someone else, a why
then not on Mathematica Help?
There are evidently three different types of string patterns used in
Mathematica, namely "Abbreviated String Patterns", "String Patterns", and
"Regular Expressions". They can be mixed in the same expression.

"Abbreviated String Patterns" are mentioned in "Some General Notations and
Conventions" and in the help for StringMatchQ, but not in
"guide/StringPatterns", in "tutorial/StringPatterns" or in the help for
StringExpression. They ought to be.

In the help for StringPosition it is said that "The string expression patt
can contain any of the objects specified in the notes for StringExpression",
but it is not mentioned that "Abbreviated String Patterns" could not be
used.

A string pattern is evidently something that depends on the context: A
string pattern valid for StringMatchQ does need to be valid for
StringPosition, and vice versa.

Evidently this is a case, where the creators of Mathematica have accepted a
convention from other systems, and then have not managed to make a
consequent and systematic notation for Mathematica.

By the way, other examples of strings not matching themselves are "\\@" and
strings containing "\\*" or "\\@".

In some of the answers it has been said that \ and * are special characters.
But they are evidently special characters very different ways: \ is always a
special character, while * (and @) are special characters just in
"Abbreviated String Patterns", if I have understood it properly.

A last question: would it break any logic to have "\\*" matching itself
always?

Best regards

Ingolf Dahl
Sweden

> -----Original Message-----
> From: ingol...@telia.com [mailto:ingol...@telia.com]
> Sent: den 7 mars 2009 08:43
> To: math...@smc.vnet.net
> Subject: When a string does not match itself
>
> 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
>

> ingol...@telia.com
>
>
>
>

Reply all
Reply to author
Forward
0 new messages