Home > Enterprise >  Add substring at specified position within a string if a pattern is identified in it except for the
Add substring at specified position within a string if a pattern is identified in it except for the

Time:01-05

import re, datetime

input_text = 'del dia 10 a las 10:00 am hasta el 15 de noviembre de 2020' #example 1
input_text = 'de el 10 hasta el 15 a las 20:00 pm de noviembre del año 2020' #example 2
input_text = 'desde el 10 hasta el 15 de noviembre del año 2020' #example 3
input_text = 'del 10 a las 10:00 am hasta el 15 a las 20:00 pm de noviembre de 2020' #example 4



identificate_day_or_month = r"\b(\d{1,2})\b"

identificate_hours = r"[\s|]*(\d{1,2}):(\d{1,2})[\s|]*(?:a.m.|a.m|am|p.m.|p.m|pm)[\s|]*"

months = r"(?:enero|febrero|marzo|abril|mayo|junio|julio|agosto|septiembre|octubre|noviembre|diciembre|este mes|mes que viene|siguiente mes|mes siguiente|mes pasado|pasado mes|anterior año|mes anterior)"

identificate_years = r"(?:del[\s|]*del[\s|]*año|de[\s|]*el[\s|]*año|del[\s|]*del[\s|]*ano|de[\s|]*el[\s|]*ano|del|de)[\s|]*(?:el|)[\s|]*(?:este[\s|]*año[\s|]*\d*|este[\s|]*año|año[\s|]*que[\s|]*viene|siguiente[\s|]*año|año[\s|]*siguiente|año[\s|]*pasado|pasado[\s|]*año|anterior[\s|]*año|año[\s|]*anterior|este[\s|]*ano[\s|]*\d*|este[\s|]*ano|ano[\s|]*que[\s|]*viene|siguiente[\s|]*ano|ano[\s|]*siguiente|ano[\s|]*pasado|pasado[\s|]*ano|anterior[\s|]*ano|ano[\s|]*anterior|este[\s|]*\d*|año \d*|ano \d*|el \d*|\d*)"



#Identification pattern conformed to the sequence of characters with which I am trying to define the search pattern
identification_re_0 = r"(?:(?<=\s)|^)(?:desde[\s|]*el|desde|del|de[\s|]*el|de )[\s|]*(?:día|dia|)[\s|]*"   identificate_day_or_month   identificate_hours   r"[\s|]*(?:hasta|al|a )[\s|]*(?:el|)[\s|]*"   identificate_day_or_month   identificate_hours   r"[\s|]*(?:del|de[\s|]*el|de)[\s|]*(?:mes|)[\s|]*(?:de|)[\s|]*(?:"   identificate_day_or_month   r"|"   months   r"|este mes|mes[\s|]*que[\s|]*viene|siguiente[\s|]*mes|mes[\s|]*siguiente|mes[\s|]*pasado|pasado[\s|]*mes|anterior[\s|]*mes|mes[\s|]*anterior)[\s|]*"   r"(?:"   identificate_years   r"|"   r")"


#Replacement in the input string by a string with built-in corrections where necessary
input_text = re.sub(identification_re_0, 
                    lambda m: , 
                    input_text, re.IGNORECASE)


print(repr(input_text))  # --> output

I was trying to get that if the pattern identification_re_0 is found incomplete, that is, without the times indicated, then it completes them with "a las 00:00 am", which represents the beginning of that indicated day with that date.

Within the same input string there may be more than one occurrence of this pattern where this procedure must be performed, therefore the number of replacements in the re.sub() function has not been limited. And I have added the re.IGNORECASE flag since capital letters should not have relevance when performing time recognition within a text.

And the correct output in each of the cases should be like this.

'del dia 10 a las 10:00 am hasta el 15 a las 00:00 am de noviembre de 2020'  #for the example 1

'de el 10 a las 00:00 am hasta el 15 a las 20:00 pm de noviembre del año 2020'  #for the example 2

'desde el 10 a las 00:00 am hasta el 15 a las 00:00 am de noviembre del año 2020'  #for the example 3

'del 10 a las 10:00 am hasta el 15 a las 20:00 pm de noviembre de 2020'  #for the example 4, NOT modify

In example 1 , "a las 00:00 am" has been added to the first date (reading from left to right).

In example 2 , "a las 00:00 am" has been added to the second date.

And in example 3, "a las 00:00 am" has been added to both dates that make up the time interval.

Note that in example 4 it was not necessary to add anything, since the times associated with the dates are already indicated (following the model pattern).

CodePudding user response:

you can capture the part of string which has to be replaced and then replace them as in orginal text.

In regex, (?!a\slas) will validate next words not same as a las.

sample code

import re


def replacer(string, capture_data, replaced_data):
    for i in range(len(capture_data)):
        string = string.replace(capture_data[i], replaced_data[i])
    return string


text = 'del dia 10 a las 10:00 am hasta el 15 de noviembre de 2020'
text1 = 'de el 10 hasta el 15 a las 20:00 pm de noviembre del año 2020'  # example 2
text2 = 'desde el 10 hasta el 15 de noviembre del año 2020'  # example 3
text3 = 'del 10 a las 10:00 am hasta el 15 a las 20:00 pm de noviembre de 2020'
re_exp = r'[A-Za-z] \s\d{2}\s(?!a\slas)'

capture_data = re.findall(re_exp, text3)
replaced_data = [i   "a las 00:00 am " for i in capture_data]
print(replacer(text3, capture_data, replaced_data))

>>> del 10 a las 10:00 am hasta el 15 a las 20:00 pm de noviembre de 2020
  • Related