How can I exclude a word by using re?
Peter Otten
__peter__ at web.de
Mon Aug 15 01:55:05 EDT 2005
could ildg wrote:
> But what should I do if there are more than one hello and I only want
> to extract what's before the first "hello". For example, the raw
> string is "hi, how are you? hello I'm fine, thank you hello. that's it
> hello", I want to extract all the stuff before the first hello?
The simplest solution is to use str.split():
>>> helo = "hi, how are you? HELLO I'm fine, thank you hello. that's it"
>>> helo.split("hello", 1)[0]
"hi, how are you? HELLO I'm fine, thank you "
But regular expressions offer a similar feature:
>>> re.compile("hello", re.IGNORECASE).split(helo, 1)[0]
'hi, how are you? '
Peter
More information about the Python-list
mailing list