Using python I'm parsing several strings. Sometimes the string has appended several semicolons to it.
Example strings:
s1="1;Some text"
s2="2;Some more text;;;;"
The number of appending semicolons varies, but if it's there it's never less than two.
The following pattern matches s1, with s2 it includes the appended semicolons.
How do I redo it to remove those?
pat=re.compile('(?m)^(\d+);(.*)')
You can use the str.rstrip([chars])
This method returns a copy of the string in which all chars have been stripped from the end of the string (default whitespace characters).
e.g. you can do:
s2 = s2.rstrip(";")
You can find more information here.
pat = re.compile(r'\d+;[^;]*')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With