-2
a = "This is some text"
b = "This text"

I want to compare the variable b in a and should return true even if the text contains more than two words like

a = "This is another text here"
b = "This another here"

even if I compare b in a should return true in python

Condition: True == All words in b found, the same order, in a.
In the above examples, the words have to be in the same order and upper/lowercase.

3
  • are they letters or words count is greater than 2? Commented Sep 14, 2018 at 10:11
  • Possible duplicate of Python String Formats with SQL Wildcards and LIKE Commented Sep 14, 2018 at 10:45
  • i want to search like this - This%text in python Commented Sep 14, 2018 at 10:53

2 Answers 2

1
a = "This is some text"
b = "This text"

c = 0 
for i in b.split(): # check for each word in a
    if i in a: c = c + 1 # if found increases the count
c>=2 # true

or

len([value for value in a.split() if value in a.split()]) >= 2
Sign up to request clarification or add additional context in comments.

1 Comment

i want it in regular expression. similar to like in mysql
0

You can mimic the behavior to a certain extent using regex.

import re

def is_like(text,pattern):
    if '%' in pattern:
        pattern = pattern.replace('%','.*?')
    if re.match(pattern,text):
        return True
    return False


a = "This is another text here"
b = "This%another%here"
print(is_like(a,b))
>>>>True
a = "Thisis sometext"
b = "This % text"
print(is_like(a,b))
>>>>False
a = "Thisis sometext"
b = "This%text"
print(is_like(a,b))
>>>>True
a = "This is some text"
b = "This text"
print(is_like(a,b))
>>>>False

Note that I have not implemented any escaping for % character, so searching for % won't work.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.