How do you use a regex in a list comprehension in Python?

后端 未结 2 455
情歌与酒
情歌与酒 2021-01-04 17:56

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

相关标签:
2条回答
  • 2021-01-04 18:35

    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)]
    
    0 讨论(0)
  • 2021-01-04 18:57

    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)]
    
    0 讨论(0)
提交回复
热议问题