Home > Back-end >  Why does this replace function fail inside this lambda function but not outside it?
Why does this replace function fail inside this lambda function but not outside it?

Time:12-27

import re

input_text = "el dia 2022-12-23 o sino el dia 2022-09-23 10000-08-23"  #example

date_capture_pattern = r"([12]\d*-[01]\d-[0-3]\d)(\D*?)"

#input_text = re.sub(date_capture_pattern , lambda m: print(repr(m[1])) , input_text)
input_text = re.sub(date_capture_pattern , lambda m: m[1].replace("_-_", "-", 2) , input_text)

print(repr(input_text))

Using a print() I have noticed that the capture groups m[1] capture them correctly, since it is able to print the 3 dates correctly.

However, I feel that there is something in the syntax (in python) of the lambda function lambda m: m[1].replace("_-_", "-", 2) that does not allow the replacement to be possible, that is, even if the lambda function correctly receives the information, it is not able to return it.

The output that I need is that

"el dia 2022_-_12_-_23 o sino el dia 2022_-_09_-_23 10000_-_08_-_23"

It should be clarified that the code as it is in the question does not generate any error in the console, however it does not work correctly since it is simply limited to deleting the capture groups within the original string

What is wrong with that lambda function?

CodePudding user response:

lambda m: m[1].replace("-","_-_", 2) , input_text)

Order should be "-" then "_-_"

You can also achieve this with a simple string function replace() .

Since your input_text is a string.

input_text.replace("-","_-_")

CodePudding user response:

Your replacement logic is off. You should be searching - and replacing with _-_. Use this version:

input_text = "el dia 2022-12-23 o sino el dia 2022-09-23 10000-08-23"
output = re.sub(r'[12]\d*-[01]\d-[0-3]\d', lambda m: m.group().replace("-", "_-_"), input_text)
print(output)

# el dia 2022_-_12_-_23 o sino el dia 2022_-_09_-_23 10000_-_08_-_23

Also note that there is no need to be using a capture group here.

CodePudding user response:

Is there a reason for the lambda function ? A simple replace should be suffice.

Code:

input_text = "el dia 2022-12-23 o sino el dia 2022-09-23 10000-08-23"

output = input_text.replace('-', '_-_')
print(output)

Output:

el dia 2022_-_12_-_23 o sino el dia 2022_-_09_-_23 10000_-_08_-_23
  • Related