PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, November 6, 2022

[FIXED] How to put '0' in front a day number only if it is one number and not two with this regexs?

 November 06, 2022     python, python-3.x, regex, regex-group, replace     No comments   

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
Answer Checked By - Candace Johnson (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

1,208,663

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © 2025 PHPFixing