Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find a string inside another string using re

I know this question has been asked before but earlier today I found the following code in SO:

import re   

def findIfWordMatch(w):
    return re.compile(r'\b({0})\b'.format(w), flags=re.IGNORECASE).search

For example if I use the following strings the function return a match object if text1 is found in text2 (otherwise the function return None):

text1 = 'DIBUJO'
text2 = 'DIBUJO B308'

So to know if text1 is in text2 I do the following:

if(findIfWordMatch(text1)(text2) is not None):  
    #doSomething()

And it has been working well until I used these variables:

text1 = 'INT.EST.C.S.'
text2 = 'INT.EST.C.S. B308'

Im almost sure it has nothing to do with the dots because I have other variables with a similar structure and in works just fine so..

I would like to know why is this happening or another way to find if a string is inside another.

Thanks in advice

like image 813
John Ackerman Avatar asked Apr 12 '26 13:04

John Ackerman


1 Answers

'INT.EST.C.S. B308'
            ^^

Together dot and space '. ', in regex equivalent to \W\W, are not considered a part of word boundary \b (^\w|\w$|\W\w|\w\W). Use negative lockahead (?<!)(?!).

Regex: (?<!\S){0}(?!\S)

like image 67
Srdjan M. Avatar answered Apr 15 '26 03:04

Srdjan M.