how to match whole word

Gary Herron gherron at islandtraining.com
Wed Jul 16 01:29:20 EDT 2008


Peng Yu wrote:
> Hi,
>
> The following code snippet is from /usr/bin/rpl. I would like the it
> to match a word, for example, "abc" in ":abc:". But the current one
> would not match "abc" in ":abc:". I tried to modify it myself. Would
> you please let me know what is the corrected way to do it?
>
> Thanks,
> Peng
>
> 	if opts.whole_words:
> 		regex = re.compile(r"(?:(?<=\s)|^)" + re.escape(old_str) + r"(?=\s|
> $)",
> 						   opts.ignore_case and re.I or 0)
> --
> http://mail.python.org/mailman/listinfo/python-list
>   

The regular expression "\w+" will match (what might be your definition 
of) a word, and in particular will match abc in :abc:.   Regular 
expressions have lots of other special \-sequences that might be worth 
your while to read about:  http://docs.python.org/lib/re-syntax.html

Gary Herron





More information about the Python-list mailing list