Questions & Answers

what is causing a syntax error for this python regex match?

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:
        return'\d{2,4}[\-?\/?.?]\d{2}[\-?\/?.?]\d{2,4}', s)
    except Exception as 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...
2023-01-18 00:30:11
Can you give a minimal example of 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
2023-01-18 00:30:11
That doesn't seem like a Python error but rather a SQL error. Where do you see that error message?
2023-01-18 00:30:11
On a second look I guess you are passing the returned object as a parameter to a query but that object is not a string. You need to use date_match(x).string to get the matched string.
2023-01-18 00:30:11
@user19308385: No, .string retrieves the entire original string passed to, not just the matching part. The matching part is .group().
Answers(0) :