What is the difference between shlex.split () and re.split ()?

So, I recently used shlex.split () to split the command as an argument to the subprocess.Popen () function. I recalled that back, I also used the re.split () function to split the string with the specified specific delimiter. Can anyone point out what is the significant difference between the two? In which scenario is each function best suited?

+6
source share
1 answer

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.

+9
source

All Articles