Home > database >  Python 3 re.sub returns unprintable characters instead of string
Python 3 re.sub returns unprintable characters instead of string

Time:09-15

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"

Edit: date is actually a string

CodePudding user response:

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.

  • Related