0
import re

def date_to_internal_stored_format(input_text, identify_only_4_digit_years = False, limit_numbers_immediately_after_date = True):

    #grupo de captura para fechas en donde el año tiene si o si 4 digitos
    if (identify_only_4_digit_years == True): 
        if(limit_numbers_immediately_after_date == True):
            date_capture_pattern = r"([12]\d{3}-[01]\d-[0-3]\d)(\D*?)"
        elif(limit_numbers_immediately_after_date == False):
            date_capture_pattern = r"([12]\d{3}-[01]\d-[0-3]\d)"

    input_text = re.sub(date_capture_pattern, lambda m: m.group().replace("-", "_-_", 2), input_text)

    return input_text


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


input_text = date_to_internal_stored_format(input_text, False, True)

print(repr(input_text))

Why doesn't the limit imposed behind work so that after the last number of the date there can be no more numbers to be considered a group that must be captured?

I need this output where 2022-12-23 is not captured I need this output where this is not captured, since there are more than 2 digits at the end of the date (\D*?)

'el dia 2022-12-2344 o sino el dia 2022_-_09_-_23 10000_-_08_-_23'
Matt095
  • 857
  • 3
  • 9
  • I don't know why you expected `(\D*?)` to do any of the things you expected it to do, but it sounds like you have a wildly off-base idea of what `*?` means. – user2357112 Dec 26 '22 at 18:21
  • I was only looking to place a limit that prevents me from capturing the date if it is followed by more numerical digits without a space or something in between – Matt095 Dec 26 '22 at 18:24
  • 1
    Maybe you are looking for *word boundaries* `\b` ([regex101 demo](https://regex101.com/r/0Z3P1v/1)). Regex101 is generally a nice place to play with the regexes - there is explanation of each token on the right side. – bobble bubble Dec 26 '22 at 19:46
  • 1
    @bobblebubble Thank you very much, I was trying to do it in another way, but putting the word boundary \b works the same way that I needed – Matt095 Dec 26 '22 at 19:52

0 Answers0