Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python 3 re.sub returns unprintable characters instead of string

I’m trying to convert dates in dd/mm/yy format to dd/mm/20yy format using re.sub and capture groups.


    date = "25/11/20"
    fixed_date = re.sub(r"(\d\d/\d\d/)(\d\d)", r"\120\2", date)

However, even though my regex seems to work on regex101.com, Python returns an imprintable character.

fixed_date
Out[42]: 'P20'

How can I get my string? In this case, it would be "25/11/2020"

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Edit: date is actually a string

>Solution :

Do

fixed_date = re.sub(r"(\d\d\/\d\d\/)(\d\d)", r"\g<1>20\g<2>", date)

From re docs:

In string-type repl arguments, in addition to the character escapes and backreferences described above, \g<name> will use the substring matched by the group named name, as defined by the (?P<name>...) syntax. \g<number> uses the corresponding group number; \g<2> is therefore equivalent to \2, but isn’t ambiguous in a replacement such as \g<2>0. \20 would be interpreted as a reference to group 20, not a reference to group 2 followed by the literal character '0'. The backreference \g<0> substitutes in the entire substring matched by the RE.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading