#!/usr/bin/env python import re string = "&(/Term/SemanticType/@cdr:ref ==" pattern = re.compile(r"[()|&]|[=!+-]=|[^\s()|&=!+-]+") print(pattern.findall(string)) pattern = re.compile(r"""\ [()|&] # single-character tokens | [=!+-]= # multi-character operators | [^\s()|&=!+-]+ # path value """, re.VERBOSE) print(pattern.findall(string)) print(re.findall(r"[ ]XXX[ ]", " XXX ")) print(re.findall(r"""\ [ ]XXX[ ] """, " XXX ", re.VERBOSE)) print(re.findall(r"\sXXX\s", " XXX ")) print(re.findall(r"""\ \sXXX\s """, " XXX ", re.VERBOSE))