looking for a regular expression

Duncan Booth duncan.booth at invalid.invalid
Tue Aug 1 09:52:24 EDT 2006


¨ì©³¦b²Ö¤°»ò°Ú¡H wrote:

> I want "justice" and the words between the 2 nearest commas, which is
> 
>  establish justice
> 
> All I can come up with is
> 
>   r",(.*?justice.*?),"
> 
> but the result is
> 
>    in order to form a more perfect union, establish justice
> 
> Apreciate any help.

",([^,]*justice[^,]*),"

But this is simpler if you don't use a regular expression:

>>> s = "We the people of the United States, in order to form a more 
perfect union, establish justice, insure domestic tranquility,......"
>>> [phrase for phrase in s.split(',') if 'justice' in phrase ]
[' establish justice']



More information about the Python-list mailing list