-2

Given regex

"(?P\<year\>\\d{4})_(?P\<month\>\\d{2})_(?P\<day\>\\d{2})"

and dictionary

{year: 2024, month: 01, day: 01}

I want to produce string

2024_01_01

I have tried re.sub but it seems to require a dummy string where you want to replace values. Could there be a better suited alternative to this?

10
  • 4
    Why do you want to use regex for this? Commented Aug 2, 2024 at 16:38
  • 2
    That dictionary has invalid syntax. Did you mean {'year': '2024', 'month': '01', 'day': '01'}? Commented Aug 2, 2024 at 16:44
  • This question is similar to: Reversing a regular expression in Python. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. Commented Aug 2, 2024 at 16:46
  • 1
    @wjandrea How about Python generate string based on regex format, which was also closed as a duplicate of the aforementioned question? Commented Aug 2, 2024 at 16:52
  • 3
    @jsbueno In the case of an XY problem, the OP should be asked to clarify their intent. Commented Aug 2, 2024 at 17:02

2 Answers 2

1

You don't need RegEx for this if you already have the proper info in a dictionary (it's likely not the right tool anyway).

Note: that dictionary is invalid as-written. For this example I've converted its keys and values into strings

# here is your dict (formatted as strings)
date_dict = {'year': '2024', 'month': '01', 'day': '01'}
# use the 'str.join' method to concatenate the dict values w/ your desired separator
date_str = '_'.join(date_dict.values())
print(date_str)
# >>> 2024_01_01
Sign up to request clarification or add additional context in comments.

Comments

1

Regular expressions are a usuall solution to parse strings, and may be poorly suited to render strings back.

It should be noted that the regular f-strings (or str.format method) in Python, along with the custom text-formatting options allowed by the datetime-module can be a better choice there.

If your date is actually in a dictionary, then it can work like this:

date_dct = {"year": 2024, "month": 1, "day": 1}
message = "{year}_{month:02d}_{day:02d}".format_map(date_dct)

Or, if you get your date as a date/datetime object:

from datetime import date
date_dct = {"year": 2024, "month": 1, "day": 1}

# Parse your dict data into a proper date object:
day = date(**date_dct)

# Use the "strftime" format specification to render date using an f-string
print(f"{day:%Y_%m_%d}")

Note that in these approaches, the date information should be used as numbers, instead of strings. If they are indeed as strings and you don't want to parse the proper date, an string formatting operation can work directly as:

message = "{year}_{month}_{day}".format_map(date_dct)"

With no information needed about the number of digits for each component.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.