re Insanity

Tim Daneliuk tundra at tundraware.com
Sun Jan 23 13:58:21 EST 2005


Orlando Vazquez wrote:

> Tim Daneliuk wrote:
> 
>> For some reason, I am having the hardest time doing something that should
>> be obvious.  (Note time of posting ;)
>>
>> Given an arbitrary string, I want to find each individual instance of
>> text in the form:  "[PROMPT:optional text]"
>>
>> I tried this:
>>
>>     y=re.compile(r'\[PROMPT:.*\]')
>>
>> Which works fine when the text is exactly "[PROMPT:whatever]" but
>> does not match on:
>>
>>    "something [PROMPT:foo] something [PROMPT:bar] something ..."
>>
>> The overall goal is to identify the beginning and end of each [PROMPT...]
>> string in the line.
>>
>> Ideas anyone?
> 
> 
> If I understand correctly, this is what you are trying to achieve:
> 
>  >>> import re
>  >>> temp = "something [PROMPT:foo] something [PROMPT:bar] something ..."
>  >>> prompt_re = re.compile(r"\[PROMPT:.*?\]")
>  >>> prompt_re.findall(temp)
> ['[PROMPT:foo]', '[PROMPT:bar]']
>  >>>
> 
> HTH,
> 
> -- 
> Orlando

Yes - that seems to be the simplest solution to the problem.  I'd forgotten
entirely about non-greedy matching when I asked the question.  Thanks.

-- 
----------------------------------------------------------------------------
Tim Daneliuk     tundra at tundraware.com
PGP Key:         http://www.tundraware.com/PGP/



More information about the Python-list mailing list