Search in a string and obtain the 2 words before and after the match in Python

后端 未结 4 2035
伪装坚强ぢ
伪装坚强ぢ 2021-01-19 15:39

I\'m using Python to search some words (also multi-token) in a description (string).

To do that I\'m using a regex like this

    result = re.search(w         


        
4条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-19 16:20

    Based on your clarification, this becomes a bit more complicated. The solution below deals with scenarios where the searched pattern may in fact also be in the two preceding or two subsequent words.

    line = "Parking here is horrible, here is great here is mediocre here is here is "
    print line
    pattern = "here is"
    r = re.search(pattern, line, re.IGNORECASE)
    output = []
    if r:
        while line:
            before, match, line = line.partition(pattern)
            if match:
                if not output:
                    before = before.split()[-2:]
                else:    
                    before = ' '.join([pattern, before]).split()[-2:]
                after = line.split()[:2]
                output.append((before, after))
    print output
    

    Output from my example would be:

    [(['Parking'], ['horrible,', 'here']), (['is', 'horrible,'], ['great', 'here']), (['is', 'great'], ['mediocre', 'here']), (['is', 'mediocre'], ['here', 'is']), (['here', 'is'], [])]

提交回复
热议问题