[Tutor] str.split and quotes

Marilyn Davis marilyn at deliberate.com
Sat Apr 9 07:18:28 CEST 2005


On Fri, 8 Apr 2005, C Smith wrote:

> Tony wrote:
>       With Python 2.4 I get these results (all imports are factored
>       out, all give
>       the same result except for CSV which strips the "s) with
>       timeit.py:
> 
> Just a note here in terms of results.  Although the results are all the
> same and they work for the case where there is single quoted phrase with
> more than one word in it, the split_and_rejoin has problems for the case
> where there is only a single word in quotes (since the trailing quote
> that you are assuming is in another word occurs in the same word and thus
> is never found). e.g. 'I said "wow" when I saw the result' will give it
> problems.
>  
> The re expression has a problem if there is more than one quoted string
> in the string (e.g. 'if "this" and "that" are quoted'). The reason is
> that the expression is "greedy" and matches the longest string of
> characters between quote marks: in '"this" and "that"' the entire string
> will be matched rather than the first "this". The fix is to use the
> non-greedy pattern:
>  
> re.compile(r'\".*?\"|[^ ]+')

Yes, thank you.  I did discover it to be too greedy and added the ?

Thank you for giving it another thought.  This will be the best
snippet of code in my whole application.

Marilyn

>  
> Note the ? after the *
>  
> /c
>  
>  
> 
> 

-- 



More information about the Tutor mailing list