AKA
AKA

Reputation: 6925

How to create regex with multiple variables in python?

I want to create a regex in python which includes string variables to find the presence of those strings in the input sentence.

For example:

Input sentences:

In 2009, I was in Kerala. I love that place.

String 1 = I, String 2 = was

This should return me the sentence which contains I and was in that order. ie, In 2009, I was in Kerala should be returned. String1 and String2 can be anywhere in the sentence but String2 should come only after String1.

This is what I did so far:

r'([ A-Za-z0-9]*)'+string1+'([^\.!?]*)'+string2+'([^\.!?]*[\.!?])'

The problem is it detects I in in also. I don't want that. I want exactly String1 and String2.

Can anyone throw some idea on how to do this ?

Upvotes: 0

Views: 141

Answers (1)

user94559
user94559

Reputation: 60143

Based on my suggestion in the comment above, just adding \bs around the strings. I've left the rest of your regular expression as-is:

r'([ A-Za-z0-9]*)\b{string1}\b([^\.!?]*)\b{string2}\b([^\.!?]*[\.!?])'.format(
    string1=string1, string2=string2)

Upvotes: 3

Related Questions