shlex.split() intended to work as a shell separation mechanism .
This means doing things like keeping quotes, etc.
>>> shlex.split("this is 'my string' that --has=arguments -or=something") ['this', 'is', 'my string', 'that', '--has=arguments', '-or=something']
re.split() will just split into any template you define.
>>> re.split('\s', "this is 'my string' that --has=arguments -or=something") ['this', 'is', "'my", "string'", 'that', '--has=arguments', '-or=something']
Trying to define your own regular expression to work as shlex.split uselessly difficult if possible.
To really see the differences between them, you can always Use Source, Luke :
>>> re.__file__ '/usr/lib/python3.5/re.py' >>> shlex.__file__ '/usr/lib/python3.5/shlex.py'
Open these files in your favorite editor and start digging, you will find that they work in a completely different way.
source share