I\'m trying to locate all index positions of a string in a list of words and I want the values returned as a list. I would like to find the string if it is on its own, or if
You don't need to assign the result of match
back to x
. And your match should be on x
rather than list
.
Also, you need to use re.search
instead of re.match
, since your the regex pattern '\W*myString\W*'
will not match the first element. That's because test;
is not matched by \W*
. Actually, you only need to test for immediate following and preceding character, and not the complete string.
So, you can rather use word boundaries
around the string:
pattern = r'\b' + re.escape(myString) + r'\b'
indices = [i for i, x in enumerate(myList) if re.search(pattern, x)]
There are a few problems with your code. First, you need to match the expr against the list element (x
), not against the whole list (myList
). Second, in order to insert a variable in the expression, you have to use +
(string concatenation). And finally, use raw literals (r'\W
) to properly interpet slashes in the expr:
import re
myList = ['test;cow', 'one', 'two', 'three', 'cow.', 'cow', 'acow']
myString = 'cow'
indices = [i for i, x in enumerate(myList) if re.match(r'\W*' + myString + r'\W*', x)]
print indices
If there are chances that myString contains special regexp characters (like a slash or a dot), you'll also need to apply re.escape
to it:
regex = r'\W*' + re.escape(myString) + r'\W*'
indices = [i for i, x in enumerate(myList) if re.match(regex, x)]
As pointed out in the comments, the following might be a better option:
regex = r'\b' + re.escape(myString) + r'\b'
indices = [i for i, x in enumerate(myList) if re.search(regex, x)]