Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7185427
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T18:24:47+00:00 2026-05-28T18:24:47+00:00

I have a string containing date string, ‘363898,Catullus,84-11-1 BC’ How can I replace year

  • 0

I have a string containing date string, ‘363898,Catullus,84-11-1 BC’
How can I replace year 84 with 0084 using Python re?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T18:24:49+00:00Added an answer on May 28, 2026 at 6:24 pm

    Try:

    import re
    re.sub(r',(\d\d)-', r',00\1-', '363898, Catullus,84-11-1 BC')
    

    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:

    • if there are no two digits between a coma and a dash, no replacement is made. You get back your original string
    • if there are these digits, there are replace by themselves, surounded by the same comma and dash, but with additional zeros.

    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:

    In [10]: entry = '363898,Catullus,84-11-1 BC'.split(',')
    In [11]: numbers = entry[-1].split('-')
    In [12]: numbers = '-'.join([numbers[0].zfill(4)] + numbers[1:])
    In [13]: entry = entry = ','.join(entry[:-1] + [numbers])
    In [14]: entry
    Out[14]: '363898,Catullus,0084-11-1 BC'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string containing a date, and another string containing the date format
I have a string containing byte data. How can I perform an in-place conversion
I have a string containing XML document using LinqtoXML What is the best way
I have a string containing HTML and I need to replace some words to
In a Python package, I have a string containing (presumably) the name of a
I have a string containing a local date/time and I need to convert it
I have a string containing a date in the format YYYY-MM-DD . How would
I have a string containing a date which has a lot of format possibilities.
I have a function that parses a string containing a date(and/or time) e.g. 2009-12-10
In my table I have a varchar column called date containing string representations of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.