re.split测试

Definition: re.split(pattern, string, maxsplit=0, flags=0)
如下将字符串按空格分开,maxsplit是分离的次数,maxsplit=1分离一次,默认为0,不限制次数

In [67]: text
Out[67]: 'HTTP/1.1 301 Moved Permanently'


In [68]: re.split(r"\s+",text)
Out[68]: ['HTTP/1.1', '301', 'Moved', 'Permanently']


In [69]: re.split(r"\s+",text,0)
Out[69]: ['HTTP/1.1', '301', 'Moved', 'Permanently']


In [70]: re.split(r"\s+",text,1)
Out[70]: ['HTTP/1.1', '301 Moved Permanently']


In [71]: re.split(r"\s+",text,2)
Out[71]: ['HTTP/1.1', '301', 'Moved Permanently']


In [72]: re.split(r"\s+",text,3)
Out[72]: ['HTTP/1.1', '301', 'Moved', 'Permanently']


In [73]: re.split(r"\s+",text,-1)
Out[73]: ['HTTP/1.1 301 Moved Permanently']


如果用括号将正则表达式括起来,那么匹配的字符串也会被列入到list中返回;如果用非捕获分组与非分组结果一致。

In [75]: re.split('\W+', 'Words, words, words.')
Out[75]: ['Words', 'words', 'words', '']

In [76]: re.split('(\W+)', 'Words, words, words.')
Out[76]: ['Words', ', ', 'words', ', ', 'words', '.', '']

In [77]: re.split('\W+', 'Words, words, words.', 1)
Out[77]: ['Words', 'words, words.']
In [12]: re.split('((?:)\W+)', 'Words, words, words.')
Out[12]: ['Words', 'words', 'words', '']
 

如果在字符串的开始或结尾就匹配,返回的list将会以空串开始或结尾。

In [78]: re.split('(\W+)', '...words, words...')
Out[78]: ['', '...', 'words', ', ', 'words', '...', '']

如果字符串不能匹配,将会返回整个字符串的list。
In [83]: re.split("c","python is better")
Out[83]: ['python is better']


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值