Search code examples
pythonregexstring-comparison

Compare two strings and Extract value of variable data in Python


In my python script, I have a list of strings like,

birth_year = ["my birth year is *","i born in *","i was born in *"]

I want to compare one input sentence with the above list and need a birth year as output.

The input sentence is like:

Example1: My birth year is 1994.
Example2: I born in 1995

The output will be:

Example1: 1994
Example2: 1995

I applied many approaches by using regex. But I didn't find a perfect solution for the same.


Solution

  • If you change birth_year to a list of regexes you could match more easily with your input string. Use a capturing group for the year.

    Here's a function that does what you want:

    def match_year(birth_year, input):  
        for s in birth_year:
            m = re.search(s, input, re.IGNORECASE)
            if m:
                output = f'{input[:m.start(0)]}{m[1]}'
                print(output)
                break
    

    Example:

    birth_year = ["my birth year is (\d{4})","i born in (\d{4})","i was born in (\d{4})"]
    
    match_year(birth_year, "Example1: My birth year is 1994.")
    match_year(birth_year, "Example2: I born in 1995")
    

    Output:

    Example1: 1994
    Example2: 1995
    

    You need at least Python 3.6 for f-strings.