re question (perhaps a stupid misunderstanding of regex-logic)

Roman Suzi rnd at onego.ru
Tue Jul 30 07:30:23 EDT 2002


On Tue, 30 Jul 2002, Stefan Antoni wrote:

> 
> i am currently reading Jeffrey E. F. Friedl's book about regex'es, and i
> must admit that i play more with the re-module then reading further on
> ;)
> 
> thats why i ask the following question:
> 
> i made the following regex:
> m  = re.compile("[^M]*")
> 
> i think, it would be read as: "find everything except the occurences of
> the char 'M' " .
> i wrote the following code:
> 
> all = string.letters + string.digits + string.hexdigits + \
> string.whitespace
> 
> M  = re.compile("[^M]*")
> M.findall(all)
> ['abcdefghijklmnopqrstuvwxyzABCDEFGHIJKL', '', \
> 'NOPQRSTUVWXYZ01234567890123456789abcdefABCDEF\t\n\x0b\x0c\r ', '']
> 
> this gives me a list with an empty item at [1] of the list.
> the documentation says: "findall: Find all occurrences of a
> pattern in a string."
> 
> since the ^M is a negated char (german "negiert", i don't know if negated is
> right), the regex-object shouldn't give me an list-item for this match, or should
> it? 
> 
> what am i missing?

That "[^M]*" matches empty string. Use "[^M]+" to match 1 or more 
characters.
 
> thx in advance,
> 
> 

Sincerely yours, Roman A.Suzi
-- 
 - Petrozavodsk - Karelia - Russia - mailto:rnd at onego.ru -
 






More information about the Python-list mailing list