pingeyeg
asked on
Remove last occurrence of characters
I am trying to figure out how to remove the last occurrence of a string of characters at the bottom of a file. I've got the following script, but it's not doing as requested. Any ideas would be greatly appreciated.
The code I'm running this on is:
************************** *****
text
************************** *****
<code>
************************** *****
text
************************** *****
<code>
************************** *****
text
************************** *****
<code>
************************** ***** <-- this line
I want to remove only the last set of *'s.
iter = r"(\*)+"
found = re.findall(iter, finalOutput)[-1]
finalOutput.replace(found, "")
The code I'm running this on is:
**************************
text
**************************
<code>
**************************
text
**************************
<code>
**************************
text
**************************
<code>
**************************
I want to remove only the last set of *'s.
Could you please clarify:
You have a huge string spanning multiple lines.
You want to find the last occurence of one or more stars and replace them with nothing?
Do these stars have to be in a separate line?
could there be whitespace before or after these stars?
could there still be some text / code after these stars??
You have a huge string spanning multiple lines.
You want to find the last occurence of one or more stars and replace them with nothing?
Do these stars have to be in a separate line?
could there be whitespace before or after these stars?
could there still be some text / code after these stars??
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
TerryAtOpus: Unfortunately, that still leaves the last bit of *'s at the bottom.
gelonida: No space or new lines after the *'s at the bottom. There are new lines before the *'s. No text after the *'s either.
gelonida: No space or new lines after the *'s at the bottom. There are new lines before the *'s. No text after the *'s either.
ASKER
Thanks guys!
Open in new window
I also allowed for space characters after the last * characters, just in case there are some.