I have a string containing date string, ‘363898,Catullus,84-11-1 BC’
How can I replace year 84 with 0084 using Python re?
I have a string containing date string, ‘363898,Catullus,84-11-1 BC’ How can I replace year
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Try:
With
r',(\d\d)-'we capture any pair of digits (not less or more) that is between a comma and a dash. We replace the whole match with a comma, two zeros, whatever was between in the first pair of parenthesis (here the two digits), and a dash.It means:
When trying regular expressions in Python, just play with an online regular expression tester Full disclaimer: I coded this one so I’m biased.
But maybe regular expressions are not the best fit for the job. Sometimes a good use of ordinary functions is just as good: