String Replacement
Peter Pearson
pkpearson at nowhere.invalid
Tue Jan 24 13:02:30 EST 2017
On Mon, 23 Jan 2017 13:23:38 -0800 (PST), subhabangalore at gmail.com 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.
More information about the Python-list
mailing list