fork download
  1. import re
  2. pattern = re.compile(r'(\w+)?([.,#])((?(1)\w*|\w+))')
  3. strings = ['no.16', '#400', 'word1.word2', 'word', '123']
  4. for s in strings:
  5. print(s, ' -> ', pattern.findall(s))
Success #stdin #stdout 0.02s 9556KB
stdin
Standard input is empty
stdout
no.16  ->  [('no', '.', '16')]
#400  ->  [('', '#', '400')]
word1.word2  ->  [('word1', '.', 'word2')]
word  ->  []
123  ->  []