fork download
  1. import re
  2. x = 'beef tomato chicken trump Madonna'
  3. n = 5
  4. right_word = '(?:\s+(\S+))?'
  5. regex_right = r'^\S*{}'.format(n*right_word)
  6. print(regex_right)
  7. m_right = re.search(regex_right, x)
  8. if m_right:
  9. print(m_right.groups())
Success #stdin #stdout 0.01s 23352KB
stdin
Standard input is empty
stdout
^\S*(?:\s+(\S+))?(?:\s+(\S+))?(?:\s+(\S+))?(?:\s+(\S+))?(?:\s+(\S+))?
('tomato', 'chicken', 'trump', 'Madonna', None)