I'm trying to apply a regex pattern to dates in my code. I apply a function that seems to find the correct date patterns (06-12-2021) but when i execute the code it gives me a syntax error at the first two digits
This is my function
def date_match(self, s:str) -> str:
try:
return re.search('\d{2,4}[\-?\/?.?]\d{2}[\-?\/?.?]\d{2,4}', s)
except Exception as e:
print(e)
I then apply it with lambda:
df['date'] = df.col1.apply(lambda x: self.date_match(x))
But I get this error:
failed: syntax error at or near "06"
LINE 4: ...-- --', '<re.Match object; span=(19, 29), match='06-24-2022...
df
as text, so we can copy it and run your code ourselves? See minimal reproducible example and How to make good reproducible pandas examples date_match(x).string
to get the matched string. .string
retrieves the entire original string passed to re.search
, not just the matching part. The matching part is .group()
.