On Mon, 23 Jan 2017 13:23:38 -0800 (PST), [email protected] wrote:
> I have a string like
>
> "Trump is $ the president of USA % Obama was $ the president
> of USA % Putin is $ the premier of Russia%"
>
> Here, I want to extract the portions from $...%, which would be
>
> "the president of USA",
> "the president of USA",
> "the premier of Russia"
[snip]
This might get you started:
$ cat temp.py
import re
x = ("Trump is $ the president of USA % Obama was $ the pres"
"ident of USA % Putin is $ the premier of Russia%")
for y in re.finditer(r"(?P<first>[^\$]*)\$(?P<second>[^%]*)%", x):
print("'{0}' -- '{1}'".format(y.group("first"), y.group("second")))
$ python3 temp.py
'Trump is ' -- ' the president of USA '
' Obama was ' -- ' the president of USA '
' Putin is ' -- ' the premier of Russia'
--
To email me, substitute nowhere->runbox, invalid->com.
--
https://mail.python.org/mailman/listinfo/python-list