STARTimport reprint(re.split(r'\W*', '...words...'))END
The output of the above python code is:['', '', 'w', 'o', 'r', 'd', 's', '', '']
In the above output given, how the last two items in the list are empty string?
I was expecting the output to be ['', '', 'w', 'o', 'r', 'd', 's', '']
My explanation of the expected output is as follows:
- \W* matches '...' and the string splits. The string returned is empty string (''). The resultant list is [''].2.\W* matches no non-word character i.e. empty string ('') between '...'& 's' and the string splits here. The string returned is empty string as yet no non-word character scanned. The resultant list is ['', '']
- \W* doesn't match 'w'.
- \W* matches empty string between 'w'& 'o' and the string splits here. The string returned is 'w'. The resultant list is ['', '', 'w']......
- \W* matches '...' between 's'& the end_of_string_ and the string splits. The string returned is 's'. The resultant list is ['', '', 'w', 'o', 'r', 'd', 's']
- \W* matches '' between '...'& the end_of_string_ and the string splits. The string returned is ''. The resultant list is ['', '', 'w', 'o', 'r', 'd', 's', '']