StringMatchQ and Regular Expressions

  • Follow


Hi

I'm running Mathematica v6.0.2 on Windows XP.

I define a regex thus:

doStmt = RegularExpression["do(?=\\s*\\w+=)"];

and then execute:

StringCases["doindex= ", doStmt]

which brings the response

{do}

I then execute

StringMatchQ["doindex= ", doStmt]

which brings the response

False

I had expected that if StringCases found any cases, then StringMatchQ
would return True.  Obviously this is not the case, but can anyone out
there throw light upon this.

Thanks for any insights you can give me

Mark Westwood

0
Reply markc.westwood (96) 5/3/2008 10:14:32 AM

Mark Westwood wrote:
> Hi
> 
> I'm running Mathematica v6.0.2 on Windows XP.
> 
> I define a regex thus:
> 
> doStmt = RegularExpression["do(?=\\s*\\w+=)"];
> 
> and then execute:
> 
> StringCases["doindex= ", doStmt]
> 
> which brings the response
> 
> {do}
> 
> I then execute
> 
> StringMatchQ["doindex= ", doStmt]
> 
> which brings the response
> 
> False
> 
> I had expected that if StringCases found any cases, then StringMatchQ
> would return True.  Obviously this is not the case, but can anyone out
> there throw light upon this.
> 
> Thanks for any insights you can give me
> 
> Mark Westwood
> 
StringMatchQ returns true if the WHOLE string matches the given pattern 
- not just part of it.

David Bailey
http://www.dbaileyconsultancy.co.uk

0
Reply dave7033 (663) 5/5/2008 10:12:31 AM


Mark Westwood schrieb:
> Hi
> 
> I'm running Mathematica v6.0.2 on Windows XP.
> 
> I define a regex thus:
> 
> doStmt = RegularExpression["do(?=\\s*\\w+=)"];
> 
> and then execute:
> 
> StringCases["doindex= ", doStmt]
> 
> which brings the response
> 
> {do}
> 
> I then execute
> 
> StringMatchQ["doindex= ", doStmt]
> 
> which brings the response
> 
> False
> 
> I had expected that if StringCases found any cases, then StringMatchQ
> would return True.  Obviously this is not the case, but can anyone out
> there throw light upon this.
> 
> Thanks for any insights you can give me
> 
> Mark Westwood
> 

See also StringFreeQ and the section
"Properties & Relations  (3)" in its homepage.

hth,

albert

0
Reply awnl1 (201) 5/6/2008 10:50:10 AM

Ahh, I think I understand now

I had crafted my forward-looking regex to match the whole string, but
was being sloppy in my thinking about what is being matched.

My regex was intended to match "the letters d,o immediately followed
by any number of space characters followed by one or more word-
constituent characters followed by an equals sign".  Used in
StringCases this finds the first two letters of the text searched.

However, used in StringMatchQ, it fails, because the whole search text
does not match the expression.  Further, it seems that StringMatchQ
will never return True if the regex uses lookahead; when attempting to
match the whole search text there is nothing for the regex engine to
look ahead to, and never will be.

Thanks for your help, and further thought prompted by your input has
shown me how to solve the problem without lookahead anyway.

Mark



>

On 6 May, 11:50, Albert Retey <a...@arcor.net> wrote:
> Mark Westwood schrieb:
>
>
>
> > Hi
>
> > I'm running Mathematica v6.0.2 on Windows XP.
>
> > I define a regex thus:
>
> > doStmt = RegularExpression["do(?=\\s*\\w+=)"];
>
> > and then execute:
>
> > StringCases["doindex= ", doStmt]
>
> > which brings the response
>
> > {do}
>
> > I then execute
>
> >StringMatchQ["doindex= ", doStmt]
>
> > which brings the response
>
> > False

> > I had expected that if StringCases found any cases, thenStringMatchQ
> > would return True.  Obviously this is not the case, but can anyone out
> > there throw light upon this.
>
> > Thanks for any insights you can give me
>
> > Mark Westwood
>
> See also StringFreeQ and the section
> "Properties & Relations  (3)" in its homepage.
>
> hth,
>
> albert


0
Reply markc.westwood (96) 5/7/2008 11:09:46 AM

3 Replies
22 Views

(page loaded in 0.084 seconds)


Reply: