-1

Imagine if there is a list of words like words = ["hello","boy","cool"] and a user input of a string string = "helloboycool".

My question: Is there a way to use Regular Expression to find all the words in the "words" list that match a part of the input "string".

For example: list = ["123","hello","nice","red","boy"] and input = "helloniceboy". The input string has no spaces.

Using the "input" string as a search term for the regular expression, the output should be the words ["hello","nice","boy"] in a list form.

Yes, I know this could be done through simple loops. However, I just trying to work on a problem that I came across while doing my office work. So, it was worth asking this question to y'all. Also, thanks for all the answers. They are sure helpful insights. Overlapping is fine

I am fairly new to the regex thing in python.

BreakinBad
  • 11
  • 1
  • 3

3 Answers3

3

Regex was made for finding things in strings, not lists.

I would suggest looping through the list and checking if each word is inside string:

words = ["123", "hello", "nice", "red", "boy"]
string = "helloboycool"
result = []
for word in words:
    if word in string:
        result.append(word)
print(result)

You could use regex for this, but I wouldn't suggest it - this is much cleaner and faster.

Here is a version with list comprehension:

words = ["123", "hello", "nice", "red", "boy"]
string = "helloboycool"
result = [word for word in words if word in string]
Ayush Garg
  • 2,234
  • 2
  • 12
  • 28
0

If you don't need to find overlapping matches, you can turn the list into a regular expression that uses | to match alternatives. Then use re.findall() to get all the matches.

import re

words = ["123","hello","nice","red","boy"]
string = "helloniceboy"
regex = re.compile('|'.join(re.escape(x) for x in words))
result = re.findall(regex, string)

re.escape() ensures that the words will be matched literally, even if they contain characters that have special meaning in regular expressions.

If you do need to find overlapping matches, the other answer that uses if word in input in a loop will work better.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • The examples are just thought up, in real life, the presence of overlapping matches starting at the same position in the string is common. This is not a solution and not a workaround. – Wiktor Stribiżew Nov 23 '20 at 21:56
  • @WiktorStribiżew The OP didn't describe the use case, it's not clear that overlapping matches are necessary. – Barmar Nov 23 '20 at 21:58
  • OPs will never tell you every detail. From experience, I know this is not going to help anyone in real life. [**This**](https://stackoverflow.com/a/42789508/3832970) is the best approach when one has a dynamic list of words and wants to look them up in some larger text. – Wiktor Stribiżew Nov 23 '20 at 22:32
  • I don't agree with your comment. I honestly believe this helps every programmer who has just started researching about regular expressions. And thanks for your insights on the question. – BreakinBad Nov 23 '20 at 23:14
0

Does it have to be a regex? It can be done easily like this:

list = ["123", "hello", "red", "nice", "happy", "boy"]
input = "helloniceboy"

result = []
for i in list:
    if i in input:
        result.append(i)

print(result)

If it has to be regex, there is a built-in module called re which you can use.

Ciro García
  • 601
  • 5
  • 22