shlex.split()
is 设计得像外壳的分割机构一样工作 https://docs.python.org/3/library/shlex.html#shlex.split.
这意味着要做一些事情,比如尊重引用等。
>>> shlex.split("this is 'my string' that --has=arguments -or=something")
['this', 'is', 'my string', 'that', '--has=arguments', '-or=something']
re.split()
只会根据您定义的任何模式进行拆分。
>>> re.split('\s', "this is 'my string' that --has=arguments -or=something")
['this', 'is', "'my", "string'", 'that', '--has=arguments', '-or=something']
尝试定义自己的正则表达式来工作shlex.split
如果可能的话,这是不必要的复杂。
要真正看到两者之间的差异,您始终可以使用来源,卢克 http://catb.org/jargon/html/U/UTSL.html:
>>> re.__file__
'/usr/lib/python3.5/re.py'
>>> shlex.__file__
'/usr/lib/python3.5/shlex.py'
在您最喜欢的编辑器中打开这些文件并开始浏览,您会发现它们的操作方式完全不同。