Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23218

Regular expression pattern \W* matching empty string in string

$
0
0

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:

  1. \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 ['', '']
  2. \W* doesn't match 'w'.
  3. \W* matches empty string between 'w'& 'o' and the string splits here. The string returned is 'w'. The resultant list is ['', '', 'w']......
  4. \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']
  5. \W* matches '' between '...'& the end_of_string_ and the string splits. The string returned is ''. The resultant list is ['', '', 'w', 'o', 'r', 'd', 's', '']

Viewing all articles
Browse latest Browse all 23218

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>