How can I exclude a word by using re?
John Machin
sjmachin at lexicon.net
Mon Aug 15 07:56:37 EDT 2005
Bruno Desthuilliers wrote:
> could ildg a écrit :
>
>> Thank you.
>> But what should I do if there are more than one hello and I only want
>> to extract what's before the first "hello".
>
>
> Read The Fine Manual ?-)
>
>
>> 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?
>
>
> re.findall(r'^(.*)hello', your_string_full_of_hellos)
Nice try, but it needs a little refinement to do what the OP asked for:
>>> import re
>>> h = "hi g'day hello hello hello"
>>> re.findall(r'^(.*)hello', h)
["hi g'day hello hello "]
>>> re.findall(r'^(.*?)hello', h)
["hi g'day "]
>>> re.findall(r'^(.*?)hello', h)[0]
"hi g'day "
More information about the Python-list
mailing list