What RE would i use to match this whole word?

Mirko Liss mirko.liss at web.de
Sat Jul 28 20:38:30 EDT 2001


On Sat, 28 Jul 2001, G. Willoughby wrote:
> What RE would i use to match this whole word, 'Rasping'
> 
> i was using:
> 
> searchString = re.compile('Rasping', re.IGNORECASE) but this don't seem to
> be right. Im missing something! any help would be appreiciated!

Hmm, what about some examples:

Python 2.1.1 (#4, Jul 25 2001, 10:17:07) 
[GCC 2.95.2 19991024 (release)] on linux2
Type "copyright", "credits" or "license" for more information.
>>> import re
>>> pattern = re.compile('RASping', re.IGNORECASE)
>>> pattern                                       
<SRE_Pattern object at 0x81ed3c8>
>>> pattern.match("rasping racing pong")
<SRE_Match object at 0x81f1628>
>>> pattern.match("rasping racing pong").group(0)
'rasping'
>>> pattern.match("Grasping racing pong").group(0)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
AttributeError: 'None' object has no attribute 'group'
>>> print pattern.match("Grasping racing pong")
None
>>> pattern.search("Grasping racing pong").group(0)
'rasping'
>>> pattern.search("Grasping racing pong").span(0)
(1, 8)

I believe the Python Documentation and the FAQ explains
this in more detail. Python reg exps are quite similar
to Perl reg exps, so there's a huge pile of books at
the friendly bookshop in your neighborhood, as well.


regards,

Mirko




More information about the Python-list mailing list