Regex splits string and repeating stripe character

Using python, I am parsing multiple lines. Sometimes a line adds a few semicolons.

Examples of lines:

s1="1;Some text" s2="2;Some more text;;;;" 

The number of additional semicolons varies, but if it is there, then it is at least two.
The following pattern matches s1, and s2 contains the added semicolons.
How to redo it to remove them?

 pat=re.compile('(?m)^(\d+);(.*)') 
+4
source share
2 answers

You can use str.rstrip([chars])

This method returns a copy of the string in which all characters have been removed from the end of the string (white space by default).

eg. You can do:

 s2 = s2.rstrip(";") 

You can find more information here .

+8
source
 pat = re.compile(r'\d+;[^;]*') 
+1
source

Source: https://habr.com/ru/post/1411641/


All Articles