82870034 发表于 2015-4-27 09:13:00

Python正则表达式的用法(2. 替换;3.拆分)

  (作者:玛瑙河,转载请注明作者或出处,) 
   字符串替换
  1.替换所有匹配的子串


1#用newstring替换subject中所有与正则表达式regex匹配的子串
2result = re.sub(regex, newstring, subject)  2.替换所有匹配的子串(使用正则表达式对象)


1reobj = re.compile(regex)
2result = reobj.sub(newstring, subject)  
  字符串拆分
1.字符串拆分


1result = re.split(regex, subject)  
2.字符串拆分(使用正则表示式对象)


1reobj = re.compile(regex)
2result = reobj.split(subject)
页: [1]
查看完整版本: Python正则表达式的用法(2. 替换;3.拆分)