Issue
Here the input examples:
import re
input_text = "Serian 4 unidades de mermelada para el dia 04 del 8 de este año 2023" #example 1
input_text = "Hay 10 unidades para el dia 15 del 12 y seran 9 ya para el 7 de noviembre" #example 2
input_text = "Hay 10 unidades para el 15 del 1º mes del año y seran alrededor de 9 para el 7º dia del mes de noviembre" #example 3
Here is what I have done with my code, I already have the regex for the days and months, but the problem is that (\d{1,2})
is used to catch 1 or 2 numerical digits but not to limit that it catches only if there is only one numerical digit, and i needed help to get that
#for days
standard_number_of_digits_re_1 = r"(?:del dia|de el dia|del|de el|el)[\s|]*(\d{1,2})"
standard_number_of_digits_re_2 = r"(\d{1,2})[\s|]*º[\s|]*dia"
#for months
standard_number_of_digits_re_3 = r"(?:" + standard_number_of_digits_re_1 + "|" + standard_number_of_digits_re_2 + r")" + r"(?:del mes|del|de el)[\s|]*(\d{1,2})"
standard_number_of_digits_re_3 = r"(?:del mes|del|de el)[\s|]*(\d{1,2})[\s|]*(?:º[\s|]*mes del año|º[\s|]*mes)"
#replacement with this conditions, and put '0' in front a day number only if it is one number and not two(or more numbers)
# example: '1' --> '01' or '10' --> '10'
input_text = re.sub(standard_number_of_digits_re_3, r"0\1", input_text)
input_text = re.sub(standard_number_of_digits_re_4, r"0\1", input_text)
input_text = re.sub(standard_number_of_digits_re_2, r"0\1", input_text)
input_text = re.sub(standard_number_of_digits_re_1, r"0\1", input_text)
print(repr(input_text)) #output
The correct output that I need:
"Serian 4 unidades de mermelada para el dia 04 del 08 de este año 2023" #for example 1
"Hay 10 unidades para el dia 15 del 12 y seran 9 ya para el 07 de noviembre" #for example 2
"Hay 10 unidades para el 15 del 01º mes del año y seran alrededor de 9 para el 07º dia del mes de noviembre" #for example 3
Solution
but the problem is that (\d{1,2}) is used to catch 1 or 2 numerical digits but not to limit that it catches only if there is only one numerical digit
Sounds like this is what you want:
(\b\d{1,2}\b)
https://regex101.com/r/1qLNEO/1
Answered By - Artemio Ramirez
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.