looking for a regular expression
Peter Otten
__peter__ at web.de
Wed Aug 2 11:33:46 EDT 2006
¨ì©³¦b²Ö¤°»ò°Ú¡H wrote:
>> How about
>> my_string = "We the people of the United States, in order to form a
>> more perfect union, establish justice, insure domestic
>> tranquility,......"
>> print (x for x in my_string.split(",") if "justice" in x).next()
>> This isn't a regular expression, but it gives what you're looking for.
>> THN
>
> Thanks a lot! I have never thought of that.
>
> But what if there's not only commas, but also periods and semicolons? I
> want to find words between 2 near by punctuations. I think it would make
> it difficult to use split instead of regular expression.
Reenter re. Use
re.split(r"[.;\-,]", my_string)
instead of my_string.split(",").
Peter
More information about the Python-list
mailing list