Sai Kumar
Sai Kumar

Reputation: 715

How to get the use regex to match years and months?

Here's a sample data,

data_example1 = "1-19" #month, year (last two digits)
data_example2 = "12-21" #month, year (last two digits)

How can I write a regex to match this and then replace the year with zero padded.

This code seems to be working if month is single digit, how can I make it work for both one digit and two digit months. Also rather than split("-"), is there a way to use regex to zero pad the year alone.

if re.search("\d\-\d\d", data_example1):
   month, year = data_example1.split("-")
   cleaned_date = month + "-" + "00" + year # zero padded year
  

Upvotes: 2

Views: 258

Answers (2)

zr0gravity7
zr0gravity7

Reputation: 3194

Simply use an optional quantifier:

\d\d?\-\d\d

As suggested by Barmar, you could use a word boundary to prevent additional digits on either end:

\b\d\d?\-\d\d\b

An alternative here would be using re.match, or anchors, if the date is the only content in the string you are matching.

Upvotes: 2

Barmar
Barmar

Reputation: 781004

Use a word boundary \b and quantifiers to match specific numbers of digits and not allow more.

Use re.sub() with capture groups and back-references.

if re.search(r"\b\d{1,2}-\d{2}\b", data_example1):
    cleaned_date = re.sub(r'\b(\d{1,2})-(\d{2})', r'\1-00\2', data_example1)

{1,2} means 1 or 2 of the previous pattern.

Upvotes: 1

Related Questions