[Switch] for the use of the re module split method in python, pythonsplit
Note: Recently, we have been studying text processing and need to use regular cut text. Therefore, this article is very useful. Thank you for choosing the original author.
Site: http://blog.sciencenet.cn/blog-314114-775285.html
Use of the re module split method in python
3094 times read: | system category: scientific research notes
We need to use re when writing a small piece of code today. the split () method discovers a new usage that was previously unknown during use. If it finds that this usage is quite practical, record it:
>>> M = re. split ('\ d +', 'dkjj23jjjj44 ')
>>> M
['Dkjj ', 'jjjj', '']
>>> M = re. split ('(\ d +)', 'dkjj23jjjj44') # Add () to the matching part ()
>>> M
['Dkjj ', '23', 'jjjj', '44', '']
>>> M = re. split ('\ d +', 'dkjj23jjjj44as ') # Add () to the matching part ()
>>> M
['Dkjj ', 'jjjj', 'as']
>>> M = re. split ('(\ d +)', 'dkjj23jjjjj44as ')
>>> M
['Dkjj ', '23', 'jjjj', '44', 'as']
The results after () is added to the matching part are different. If () is not used, the matched items are not retained, but if () is used, the matched items are retained, this is very important in the use of some matching parts that need to be retained.