[Tutor] String matching

Gooch, John John.Gooch at echostar.com
Sat Nov 27 01:22:14 CET 2004

I am trying to extract data found in an MS Excel workbook using regular
expressions. For some reason, it plain refuses to recognize the "-" unicode
character. Here is what happens when I execute:
    print "Resolving name for "+tname+" type=",type(tname)
    rePName = re.compile( u"(-)", re.UNICODE  )
    m = rePName.match( tname )
    print "Resolved to groups"+unicode(m.groups())

Resolving name for 1.0 Windows - AB - 11/23/04 type= <type 'unicode'>
Resolved to groups(u'1.0 Windows \u2013 HD - 11/23/04',)

Notice I told it to match on the "-" character, which shows up in the print
command. But when I tell it to match on the character, it doesn't match. U
have tried the escape code "\u2013", but that does not match at all. 

Any ideas?

John A. Gooch
Systems Administrator
IT - Tools
EchoStar Satellite L.L.C.
9601 S. Meridian Blvd.
Englewood, CO  80112
Desk: 720-514-5708 

-----Original Message-----
From: Jacob S. [mailto:keridee at jayco.net] 
Sent: Friday, November 26, 2004 10:57 AM
To: Bernard Lebel
Cc: tutor at python.org
Subject: Re: [Tutor] String matching

Hi Bernard!

> Hello,
> I'm a little bit confused about the match/search methods of the string 
> module for regular expressions. I have looked the regular expression 
> doc and I can't that one simple answer.
> So please excuse the basiqueness of my question, as I am used with the 
> .match method of JScript and it kind of matches anything....
> Let say I have this string:
>  >>> import re
>  >>> mystring = 'helloworldmynameisbernardlebel'
> Now I wish to attempt a match with 'bernard'.
>  >>> if not re.compile( 'bernard' ).match( mystring ) == None: ... 
> print 'success'

First, you are printinting redundant code here. You are using double
negatives. You first check to see if re.compile('bernard').match(mystring)
registers false, if it is false the expression registers true. Then you
check to see if that expression registers false. So really the equivalent
expression is

if re.compile('bernard').match(mystring):
    print 'success'

However, I don't think that solves your problem.
> Well, nothing gets printed. Could anyone tell me how to achieve that 
> simple type of matching?

Re expressions are a little different from JScript. The match method only
matches equivalent strings. (Or it only does in this case.) Instead, you
want to use the search method. Oh, and by the way, if you're going to
compile the expression without assigning it to a variable, you might use the
re methods instead of the re pattern methods. IOW, use this code instead...

import re
mystring = 'helloworldmynameisbernardlebel'
if re.search('bernard',mystring):
    print 'success'

The documentation for the re methods are hidden, IMHO. You can find them by
typing in 'search() (in module re)' in the index and that should take you
right to it. That page contains explanations for all of the re methods.

Good luck,
Jacob Schmidt

Tutor maillist  -  Tutor at python.org

More information about the Tutor mailing list