Note: Recently in the study of text processing, need to use the regular cutting text, so received this article, very useful, thank the original author.
Original site: http://blog.sciencenet.cn/blog-314114-775285.html
About the use of the split method in the RE module in Python
has been read 3,094 times 2014-3-12 11:30 | System Category: Scientific notes
Today, when writing a small code, you need to use the Re.split () method, in the process of using the discovery of a previously unknown new usage, found that the usage is quite practical, it is recorded:
>>> m = re.split (' \d+ ', ' dkjj23jjjj44 ')
>>>m
[' Dkjj ', ' jjjj ', ']
>>> m = Re.split (' (\d+) ', ' dkjj23jjjj44 ') #匹配部分加上 ()
>>>m
[' Dkjj ', ' All ', ' jjjj ', ' 44 ', ']
>>> m = re.split (' \d+ ', ' dkjj23jjjj44as ') #匹配部分加上了 ()
>>>m
[' Dkjj ', ' jjjj ', ' as ']
>>> m = Re.split (' (\d+) ', ' dkjj23jjjj44as ')
>>>m
[' Dkjj ', ' All ', ' jjjj ', ' a ', ' as ']
After the matching section plus () The result is different, no () does not retain the matching items, but there is () can retain the matching items, this in some need to retain the matching part of the use process is very important.
"Go" about the use of the split method of the RE module in Python