Skip to content
Advertisement

Match everything except a complex regex pattern and replace it in Pandas

I have a complex regex pattern to match mixed dates for a csv column in pandas df. I would like to replace everything except the regex pattern match with “” . I have tried pretty much all the negation cases (^ ?! and others). But I keep replacing the regex match with “” (empty string). My Code:

JavaScript

Some examples of my data:

JavaScript

Expected Output

JavaScript

I would appreciate your help. Many thanks.

Advertisement

Answer

I have simplified your regex a little and am extracting rather than replacing:

Loading your data to a DataFrame:

JavaScript

Gives:

JavaScript

Now extract anything that can be parsed as a date:

JavaScript

Which returns:

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement