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. C点滴成海------Dev C++怎么修改成简体中文

    第一步:选择菜单中的Tools 第二步:选择Tools中的“Envirnoment Options”,即第二个选项 第三步:选择中文并保存 将"1"的语言改成中文就行了

  2. uniGUI HyperServer

    uniGUI HyperServer 是一种新的服务器体系架构, 旨在高度提高 uniGUI 应用程序的可用性.稳定性和特定的可伸缩性. 这一目标是通过应用业界已知和广泛使用的技术 (如负载平衡和过程 ...

  3. Tomcat jdk 项目搭建问题

    Tomcat 出现log4j未找到  是因为缺少servlet包 出现版本1.5及更高错误 是Java Compiler的版本错误 需重新导包Installed JRES.

  4. MAVEN 阿里云中央仓库

    <mirror> <id>nexus-aliyun</id> <mirrorOf>*</mirrorOf> <name>Nexu ...

  5. iOS原生和React-Native之间的交互2

    今天看下iOS原生->RN: 这里有个问题: * 我这里只能通过rn->ios->rn来是实现* 如果想直接ios-rn 那个iOS中的CalendarManager的self.br ...

  6. [转]内存分配malloc, new , heapalloc

    malloc,new,VirtualAlloc,HeapAlloc性能(速度)比较 http://www.cppblog.com/woaidongmao/archive/2011/08/12/1531 ...

  7. Python学习笔记第十一周

    目录: 1.RabbitMQ   2.Redis  内容: 1.RabbitMQ 实现简单的队列通信 send端 import pika credentials = pika.PlainCredent ...

  8. 【leetcode】283. Move Zeroes

    problem 283. Move Zeroes solution 先把非零元素移到数组前面,其余补零即可. class Solution { public: void moveZeroes(vect ...

  9. 算法训练 K好数 解析

    算法训练 K好数 时间限制:1.0s 内存限制:256.0MB 提交此题 锦囊1 锦囊2 问题描述 如果一个自然数N的K进制表示中任意的相邻的两位都不是相邻的数字,那么我们就说这个数是K好数.求L位K ...

  10. PTA——各位数之和

    PTA 7-28 求整数的位数及各位数字之和 我的程序: #include<stdio.h> #include<math.h> int main(){ ,t; scanf(&q ...