0
点赞
收藏
分享

微信扫一扫

关于python 的re.sub用法

码农K 2022-02-06 阅读 63
python
>>> import re
>>> text = "JGood is a handsome boy, he is cool, clever, and so on..."
>>> print(re.sub(r'\s+', '-', text))
JGood-is-a-handsome-boy,-he-is-cool,-clever,-and-so-on...
>>> print(re.sub(r'is\s+', '-', text))
JGood -a handsome boy, he -cool, clever, and so on...
>>> print(re.sub(r'\s+\.', '.', text))
JGood is a handsome boy, he is cool, clever, and so on...
>>> text = "JGood is a handsome boy  , he is cool  , clever  , and so on..."
>>> print(re.sub(r'\s+,\s+', ',',text))
JGood is a handsome boy,he is cool,clever,and so on...
>>> 

re.sub
  re.sub用于替换字符串中的匹配项。

import re  
  
text = "JGood is a handsome boy, he is cool, clever, and so on..."  
print re.sub(r'\s+', '-', text)
#将空格替换成—,\s代表空格。
举报

相关推荐

python re.sub

Python——re的基本用法

0 条评论