Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex split a string and strip recurring character

Tags:

python

regex

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+);(.*)')
like image 338
webern Avatar asked Jan 17 '23 17:01

webern


2 Answers

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.

like image 162
Thanasis Petsas Avatar answered Jan 30 '23 20:01

Thanasis Petsas


pat = re.compile(r'\d+;[^;]*')
like image 42
Joel Cornett Avatar answered Jan 30 '23 18:01

Joel Cornett