python中查找特定字符的方法
python中查找特定字符的方法
- in、not in
- re.findall
- re.match
- re.search
- re.sub
- re.compile和findall
- re.compile和search
- re.compile和match
- re.split
in、not in
使用in或者not in来进行判断字符串中是否包含有特定的字符
a='这个暑假的上课时间还有很长时间'
b=['暑假','上课','时间','很长']
n=0
# 使用in、not in来进行判断,字符串中有无特定的字符
for i in b:if i in a:n+=1print(i in a)
print(f'这串字符中包含的关键词的个数有{n}个')
结果
结果返回的是布尔数据
True
True
True
True
这串字符中包含的关键词的个数有4个
re.findall
使用findall来从字符串中找到对应的字符
a='这个暑假的上课时间还有很长时间,url="https://www.baidu.com"'
print(re.findall('url="(.*?)"',a))
结果
结果返回的是一个列表
['https://www.baidu.com']
re.match
使用re.match来对字符串中的特定字符进行匹配,可以得到匹配的元组
import re
fullstr = 'name:alice,result:89'
result = re.match('name:(\w+),result:(\d+)', fullstr)
print(result)#<re.Match object; span=(0, 20), match='name:alice,result:89'>
print(result.group(0))#name:alice,result:89
print(result.group(1))#alice
print(result.group(2))#89
print(result.group())#name:alice,result:89
结果
<re.Match object; span=(0, 20), match='name:alice,result:89'>
name:alice,result:89
alice
89
name:alice,result:89
re.search
re.search会匹配整个字符串,并返回第一个成功的匹配。如果匹配失败,则返回None
import re
fullstr = 'class:1班,name:alice,name:wee,result:89'
result = re.search('name:(\w+),result:(\d+)', fullstr)
print(result)#<re.Match object; span=(20, 38), match='name:wee,result:89'>
print(result.group(0))#name:wee,result:89
print(result.group(1))#wee
print(result.group(2))#89
print(result.group())#name:wee,result:89
结果
<re.Match object; span=(20, 38), match='name:wee,result:89'>
name:wee,result:89
wee
89
name:wee,result:89
re.sub
fullstr = 'name:alice,result:89'
res1 = re.sub('(?P<value>\d+)','233',fullstr)#通过定义得到的name来得到数值
print(res1)
结果
name:alice,result:233
re.compile和findall
re.compile,是将一个正则表达式中的字符串形式编译一个Pattern对象
content = 'Hello, I am Jerry, from Chongqing, a montain city, nice to meet you……'
# re.compile将正则表达式的样式编译为一个 正则表达式对象
regex = re.compile('\w*o\w*')#匹配字母数字及下划线,o 表示匹配字符o,\w* 表示匹配由大小写英文字母数字和下划线组成的0个或多个字符
x = regex.findall(content)
print(x)
re.compile和search
content = 'Hello, I am Jerry, from Chongqing, a montain city, nice to meet you……'
regex = re.compile('\w*o\w*')
y = regex.search(content)
print(y)
print(type(y))
print(y.group())
print(y.span())
结果
<re.Match object; span=(0, 5), match='Hello'>
<class 're.Match'>
Hello
(0, 5)
re.compile和match
content = 'Hello, I am Jerry, from Chongqing, a montain city, nice to meet you……'
regex = re.compile('\w*o\w*')
y = regex.match(content)
print(y)
print(type(y))
print(y.group())
print(y.span())
结果
<re.Match object; span=(0, 5), match='Hello'>
<class 're.Match'>
Hello
(0, 5)
re.split
re.split用来从字符串中得到多个特定的字符
import re
s='abc, abc, defg, dds'
re.split('wxy*',s)
结果
['abc, abc, defg, dds']
python中查找特定字符的方法相关推荐
- swif 在字符串中查找特定字符索引以及改变字符串的指定位置的颜色 字体大小
1 第一种方式 var text = "谁包含这个字母";let range:Range<String.Index> = text.range(of: "含& ...
- 在Python中查找字符串长度
介绍 (Introduction) In this tutorial, we are going to discuss how we can find string length in Python. ...
- php 查找多维数组的值_php在多维数组中查找指定值的方法
本文主要介绍了php实现在多维数组中查找特定value的方法,实例分析了php实现多维数组的遍历及unset删除的相关技巧.希望对大家有所帮助.本文实例讲述了php实现在多维数组中查找特定value的 ...
- python从键盘输入一个列表计算输出元素的平均值_python列表查找值_在Python中查找列表平均值的5种方法...
python列表查找值 Hi Folks! In this article, we will have a look at the various ways to find the average o ...
- python模糊查找文件夹名字_python实现在目录中查找指定文件的方法
本文实例讲述了python实现在目录中查找指定文件的方法.分享给大家供大家参考.具体实现方法如下: 1. 模糊查找 复制代码 代码如下: import os from glob import glob ...
- python 查找指定文件_python实现在目录中查找指定文件的方法
本文实例讲述了python实现在目录中查找指定文件的方法.分享给大家供大家参考.具体实现方法如下: 1. 模糊查找 代码如下: import os from glob import glob #用到了 ...
- 在Python中查找子字符串索引的5种方法
在Python中查找字符串中子字符串索引的5种方法 (5 Ways to Find the Index of a Substring in Strings in Python) str.find() ...
- python查找指定文件夹_python实现在目录中查找指定文件的方法
本文实例讲述了python实现在目录中查找指定文件的方法.分享给大家供大家参考.具体实现方法如下: 1. 模糊查找 复制代码 代码如下: import os from glob import glob ...
- python3 列表长度_3种在Python中查找列表长度的简便方法
python3 列表长度 In this article, we will be unveiling techniques to find the length of a Python list. F ...
最新文章
- (转)Linux下C++开发初探
- 设计模式理解:策略模式
- 维特比算法—打字输入预测
- 限界分支法(队列方式)追踪解:01背包问题
- URAL 1036 Lucky Tickets
- linux系统编程之进程(七):system()函数使用【转】
- mnist手写数字数据集_mnist手写数据集(1. 加载与可视化)
- 常用的几个PHP加密函数
- mybatis-plus 中 queryWrapper and与or嵌套
- Caffe傻瓜系列(6):solver及其配置
- 语义分割网络-Segnet
- idea 文件不识别 java文件显示J
- Atmega16驱动三轴加速度传感器MMA7455
- x86 单线并发多拨_最近好多人问单线多拨,傻瓜式openwrt单线多拨叠加速率教程...
- 将两个数组不同的元素拼接成一个字符串
- 干货分享!华为模拟器Web配置防火墙
- 一个命令就可启用的微信机器人WhoChat
- LeetCode-整数反转【解决Integer的OverFlow】
- IR780 Biotin可以和生物素进行修饰连接,IR 780生物素
- Python 网络爬虫入门详解
热门文章
- 怎样才能学好Linux
- 【罗德岛人事处】明日方舟模拟寻访PHP网站搭建
- JDK1.8 邮戳锁(StampedLock)知识点整理以及示例
- 常用群晖第三方套件源汇总
- 湖南文旅数据中心:湖南文旅数据早知道(9月1日)
- Element-UI中调用tinymce6实现本地化加载,并解决提示:This domain is not registered with TinyMCE Cloud,省去api-key
- 《Fundamentals of Wireless Communication》——信道模型
- 使用逻辑回归对泰坦尼克号数据 生存死亡情况预测
- Linux系统从零搭建泰拉瑞亚服务器
- 母亲节快乐!程序员们也不要忘记把这份礼品送给母亲