str.replace()和re.sub()/calendar.month_abbr/re.subn()/upper和lower和capitalize/贪婪匹配和费贪婪匹配/re.S和re.DOTALL 笔记
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 笔记的更多相关文章
- Pandas: 使用str.replace() 进行文本清洗
str.replace()可以一次处理一整个Series.str.replace()的正式形式为 Series.str.replace(pat, repl) ,其中pat为想要寻找的模式,一般为正则表 ...
- 使用Pandas: str.replace() 进行文本清洗
前段时间参加了Kaggle上的Mercari Price Suggestion Challenge比赛,收获良多,过些时候准备进行一些全面的总结,本篇文章先谈一个比赛中用到的小技巧. 这个比赛数据中有 ...
- Uncaught TypeError: str.replace is not a function
在做审核页面时,点击审核通过按钮不执行 后来F12控制台查看发现有报错 是因为flisnullandxyzero未执行 然后找出这个方法,此方法为公共方法,将这个方法复制出来 然后使用console. ...
- str.replace替换变量名的字符串
网易云课堂该课程链接地址 https://study.163.com/course/courseMain.htm?share=2&shareId=400000000398149&cou ...
- js实现千位分隔符——str.replace()用法
/*js*/function commafy(num){ return num && num.toString().replace(/(\d{1,3})(?=(\d{3})+(?:$| ...
- Python 个人的失误记录之str.replace
1. replace 替换列表中元素的部分内容后返回列表 2018.06.08 错误操作 -- 这样并不能改变改变列表内的元素 data = [', '决不能回复---它'] data[2].repl ...
- STL str replace
#include <iostream> #include <string> using namespace std; void main() { string s=" ...
- 【javascript杂谈】你所不知道的replace函数
前言 最近在做面试题的时候总会用到这个函数,这个函数总是和正则表达式联系到一起,并且效果很是不错,总能很简单出色的完成字符串的实际问题,大家肯定都会使用这个函数,像我一样的初学者可能对这个函数的了解还 ...
- js中replace的用法【转】
1.replace方法的语法是:stringObj.replace(rgExp, replaceText) 其中stringObj是字符串(string),reExp可以是正则表达式对象(RegExp ...
随机推荐
- 一种基于SDR实现的被动GSM嗅探
软件定义无线电(SDR)是一种无线电通信系统,简单来说,就是通过数字信号处理技术在通用可编程数字信号处理硬件平台上,利用软件定义来实现无线电台的各单元功能,从而对无线电信号进行调制.解调.测量.SDR ...
- 基于ArduinoLeonardo板子的BadUSB攻击实战
0X00 前言 在Freebuf上许多同学已经对HID攻击谈了自己的看法,如维克斯同学的<利用Arduino快速制作Teensy BadUSB>无论从科普还是实践都给我们详尽的描述了Bad ...
- 将numpy array由浮点型转换为整型
使用numpy中的astype()方法可以实现,示例如下: x Out[20]: array([[ 5. , 4. ], [ 4. , 4.33333333], [ 3.66666667, 4.5 ] ...
- 5--Selenium环境准备--firefox与geckodriver
selenium2时打开firefox浏览器是不需要安装firefoxdriver的,但是selenium3不支持向前支持火狐浏览器了,40以后版本的火狐,运行会出现问题. 1.下载geckodriv ...
- 2016ICPC-大连 A Simple Math Problem (数学)
Given two positive integers a and b,find suitable X and Y to meet the conditions: X+Y=a Least Common ...
- HDU 1796 How many integers can you find(容斥原理)
题目传送:http://acm.hdu.edu.cn/diy/contest_showproblem.php?cid=20918&pid=1002 Problem Description ...
- 异常值检测 —— MAD(median absolute deviation)
MAD 定义为,一元序列 Xi" role="presentation">XiXi 同其中位数偏差的绝对值的中位数(deviation,偏差本身有正有负): MAD ...
- Android SO UPX壳问题小记
网上有篇 Android SO(动态链接库)UPX加固指南,详细介绍了如何使用UPX给Android SO加壳,尝试做了一下结果ok,这里只记录遇到的几个小问题. 1.40k以下so不能加壳 kiii ...
- MySQL的查询语句
一.基本查询语句 1.1从单个表中查询列 语法:select 查询的列1,查询的列2 from 表名 where 条件 group by 分组条件 order by 排序条件 having ...
- galera cluster,mysql配置wsrep_notify_cmd参数,增加邮件告警
vi /usr/local/sunlight/wsrep_notify_cmd.sh chown mysql:mysql /usr/local/sunlight/wsrep_notify_cmd.s ...