fork download
  1. import itertools, re
  2.  
  3. s_list = ['abc', 'def', '0123', '10-b', '0xFF']
  4. text = 'abcd abc def 000xFF 0123 10-b 0xFF abc'
  5.  
  6. it1, it2 = itertools.tee(s_list, 2)
  7. next(it2)
  8.  
  9. pattern = '|'.join('{0}\s{{1,4}}{1}(?=\s|$)'.format(word1, word2) for word1, word2 in zip(it1, it2))
  10.  
  11. #Непересекающиеся вхождения пар
  12. print (re.findall(pattern, text))
  13.  
  14. #Если нужны пересекающиеся вхождения
  15. pattern = r'(?=(' + pattern + '))'
  16. print (re.findall(pattern, text))
Success #stdin #stdout 0.04s 9836KB
stdin
Standard input is empty
stdout
['abc  def', '0123  10-b']
['abc  def', '0123  10-b', '10-b 0xFF']