Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Use regex to replace words before any digit with nothing

I want to replace words and spaces that appear before a digit in a string with nothing. For example, for the string = ‘Juice of 1/2’, I want to return ‘1/2’. I tried the following, but it did not work.

string = "Juice of 1/2"
new = string.replace(r"^.+?(?=\d)", "")

Also I am trying to perform this on every cell of a list of columns using the following code. How would I incorporate the new regex pattern into the existing pattern of r"(|)|?

df[pd.Index(cols2) + "_clean"] = (
    df[cols2]
    .apply(lambda col: col.str.replace(r"\(|\)|,", "", regex=True))

)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

.+? will match anything, including other digits. It will also match the / in 1/2. Since you only want to replace letters and spaces, use [a-z\s]+.

You also have to use re.sub(), not string.replace() (in Pandas, .str.replace() processes regular expressions by default).

new = re.sub(r'[a-z\s]+(?=\d)', '', string, flags=re.I)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading