str.replace()可以进行简单的替换

>>> a = 'one.txt, index.py, index.php, index.html, index.js'
>>> a.replace('one.txt', 'index.css')
'index.css, index.py, index.php, index.html, index.js'

re.sub()可以使用正则替换

>>> import re
>>> a
'one.txt, index.py, index.php, index.html, index.js'
>>> re.sub(r'\.[a-z]+', '.csv', a)
'one.csv, index.csv, index.csv, index.csv, index.csv'

# re.sub还可以保留原字符串的大小写(不过要麻烦一些)

import re
text1 = 'UPPER PYTHON, lower python, Capitalize Python'
def python_deal(word):
def replace_word(m):
text = m.group() # 接收re.sub()函数第一个参数匹配的内容
# 进行word(新字符串)的转换
if text.isupper():
return word.upper()
elif text.islower():
return word.lower()
elif text[0].isupper():
return word.capitalize()
else:
return word
return replace_word
# re.IGNORECASE 可以忽略大小写的区别,还有值得注意的是re.IGNORECASE的键(flags)必须写
# re.sub()第二个参数使用函数的话,里面必须是回调函数,而且回调函数的参数必然是re.sub第一个参数匹配的内容
print(re.sub('python', python_deal('new_python'), text1, flags=re.IGNORECASE))

使用calendar.month_abbr

# 可以将字符串/数字进行转换成为对应的因为月份

>>> from calendar import month_abbr
>>> month_abbr[int('')]
'Dec'

使用re.subn()

# 进行统计进行替换的次数

import re
text1 = 'namejr04/15/1996, abc11/17/2018, qwe11/17/2017'
# 匹配时间字符
date_sub = re.compile(r'(\d+)/(\d+)/(\d+)')
new_sub, n = date_sub.subn(r'\3-\1-\2', text1) # 获取新替换的字符串和替换的次数
print(new_sub)
print(n)
"""
D:\笔记\python电子书\Python3>python index.py
namejr1996-04-15, abc2018-11-17, qwe2017-11-17
3
"""

upper()/lower()/capitalize()转换字母

>>> a = 'hello world'
>>> b = a.upper() # 将全部字母转换为大写
>>> b
'HELLO WORLD'
>>> c = b.lower() # 将全部字母转换为小写
>>> c
'hello world'
>>> d = c.capitalize() # 将首字母进行大写,其他字母转换为小写
>>> d
'Hello world'

贪婪模式和费贪婪模式

# 贪婪匹配会匹配到尽可能多的字符

>>> a
"i love 'my father' and 'my mother' and 'my brothers'"
>>> re.findall(r'\'(.*)\'', a) # 匹配到的"'"是最前面一个和最后面一个
["my father' and 'my mother' and 'my brothers"] # 非贪婪匹配匹配到的尽可能少的字符(满足匹配条件的情况下)
>>> a
"i love 'my father' and 'my mother' and 'my brothers'"
>>> re.findall(r'\'(.*?)\'', a) # 匹配到的是每一对单引号里面的内容
['my father', 'my mother', 'my brothers']

使用re.S/re.DOTALL进行换行符的匹配

import re
a = """*onrtsdddddddddd
onehellow
addd
*"""
# 使用re.S和re.DOTALL匹配出来的内容都是一样的,都表示包括换行符内容的匹配
str_patter1 = re.compile(r'\*(.*)\*', re.DOTALL)
print(str_patter1.findall(a))
"""
D:\笔记\python电子书\Python3>python index.py
['onrtsdddddddddd\nonehellow\naddd\n']
"""

str.replace()和re.sub()/calendar.month_abbr/re.subn()/upper和lower和capitalize/贪婪匹配和费贪婪匹配/re.S和re.DOTALL 笔记的更多相关文章

  1. Pandas: 使用str.replace() 进行文本清洗

    str.replace()可以一次处理一整个Series.str.replace()的正式形式为 Series.str.replace(pat, repl) ,其中pat为想要寻找的模式,一般为正则表 ...

  2. 使用Pandas: str.replace() 进行文本清洗

    前段时间参加了Kaggle上的Mercari Price Suggestion Challenge比赛,收获良多,过些时候准备进行一些全面的总结,本篇文章先谈一个比赛中用到的小技巧. 这个比赛数据中有 ...

  3. Uncaught TypeError: str.replace is not a function

    在做审核页面时,点击审核通过按钮不执行 后来F12控制台查看发现有报错 是因为flisnullandxyzero未执行 然后找出这个方法,此方法为公共方法,将这个方法复制出来 然后使用console. ...

  4. str.replace替换变量名的字符串

    网易云课堂该课程链接地址 https://study.163.com/course/courseMain.htm?share=2&shareId=400000000398149&cou ...

  5. js实现千位分隔符——str.replace()用法

    /*js*/function commafy(num){ return num && num.toString().replace(/(\d{1,3})(?=(\d{3})+(?:$| ...

  6. Python 个人的失误记录之str.replace

    1. replace 替换列表中元素的部分内容后返回列表 2018.06.08 错误操作 -- 这样并不能改变改变列表内的元素 data = [', '决不能回复---它'] data[2].repl ...

  7. STL str replace

    #include <iostream> #include <string> using namespace std; void main() { string s=" ...

  8. 【javascript杂谈】你所不知道的replace函数

    前言 最近在做面试题的时候总会用到这个函数,这个函数总是和正则表达式联系到一起,并且效果很是不错,总能很简单出色的完成字符串的实际问题,大家肯定都会使用这个函数,像我一样的初学者可能对这个函数的了解还 ...

  9. js中replace的用法【转】

    1.replace方法的语法是:stringObj.replace(rgExp, replaceText) 其中stringObj是字符串(string),reExp可以是正则表达式对象(RegExp ...

随机推荐

  1. shell日常实战练习——通过监视用户登陆找到入侵者

    #!/usr/bin/bash #用户检测入侵工具 AUTHLOG=/var/log/secure if [[ -n $1 ]];then AUTHLOG=$1 echo "Using Lo ...

  2. vue 缩水版 双向绑定

    function Observer(obj, key, value){ var dep = new Dep(); if (Object.prototype.toString.call(value) = ...

  3. 深入了解HyperServer

    本文,我们将尝试深入了解uniGUI HyperServer. 可以将HyperServer所有功能分成三类: HyperServer和稳定性 HyperServer和可扩展性 HyperServer ...

  4. js第一天学习内容

    var a=12: var t=(- -a)-(a- -)+(a++)-(a++) console.log(a) t=-1: a=12: (- -a)=11-(a=11- -)=10+(a=10++) ...

  5. Ubuntu 16.04安装vsftpd 并开启ftp服务

    1. 安装 sudo apt-get install vsftpd 2.可以使用下列命令来打开,关闭,重启ftp服务 sudo /etc/init.d/vsftpd start sudo /etc/i ...

  6. vue--http请求的封装--session

    export function Fecth (url, data, file, _method) { if (file) { // 需要上传文件 return new Promise((resolve ...

  7. VTP管理交换机的VLAN配置

    实验要求:将Switch1设置为VTPserver.Switch2设置为VTPtransparent.Swtich3和4设置为VTPclient 拓扑如下: 涉及内容: 1.VTP的创建 2.VTP的 ...

  8. react-native “Unable to resolve module 'AccessibilityInfo'” 的解决方案

    执行 react-native run-android 安装Android APP后却是一屏大红幕报 Unable to resolve module ‘AccessibilityInfo’ 的异常 ...

  9. git get submodule after clone

    /********************************************************************************* * git get submodu ...

  10. MyBatis #{} 取值注意事项

    正确写法#{key} 错误写法#{key } #{}中不能加空格,不然会报错