Skip to content Skip to sidebar Skip to footer

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 an

Solution 1:

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:

defmatch_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:1994Example2:1995

You need at least Python 3.6 for f-strings.

Solution 2:

str1=My birth year is 1994.
str2=str1.replace('My birth year is ','')

You can try something like this and replace the unnecessary string with empty string.

For the code you shared, you can do something like :

for x in examples:for y in birth_year:ifx.find(y)==1:#checking if the substring exists in examplex.replace(y,'')#if it exists we replace it with empty string 

I think the above code might work

Solution 3:

If you can guaranty those "strings like" always contain one 4 digits number, which is a year of birth, somewhere in there... i'd say just use regex to get whatever 4 digits in there surrounded by non-digits. Rather dumb, but hey, works with your data.

import re

examples = ["My birth year is 1993.", "I born in 1995", "я родился в 1976м году"]
forstrin examples:
    y = int(re.findall(r"^[^\d]*([\d]{4})[^\d]*$", str)[0])
    print(y)

Post a Comment for "Compare Two Strings And Extract Value Of Variable Data In Python"