Remove all occurrences of words in a string from a python list

前端 未结 2 1875
孤独总比滥情好
孤独总比滥情好 2020-12-17 22:57

I\'m trying to match and remove all words in a list from a string using a compiled regex but I\'m struggling to avoid occurrences within words.

Current:

<         


        
相关标签:
2条回答
  • 2020-12-17 23:56

    One problem is that only the first \b is inside a raw string. The second gets interpreted as the backspace character (ASCII 8) rather than as a word boundary.

    To fix, change

    regex = re.compile(r'\b('+remove+')\b', flags=re.IGNORECASE)
    

    to

    regex = re.compile(r'\b('+remove+r')\b', flags=re.IGNORECASE)
                                     ^ THIS
    
    0 讨论(0)
  • 2020-12-17 23:59

    here is a suggestion without using regex you may want to consider:

    >>> sentence = 'word1 word2 word3 word1 word2 word4'
    >>> remove_list = ['word1', 'word2']
    >>> word_list = sentence.split()
    >>> ' '.join([i for i in word_list if i not in remove_list])
    'word3 word4'
    
    0 讨论(0)
提交回复
热议问题