python中re.findall函数实例用法

作者:小妮浅浅 时间:2021-03-28 07:51:20 

1、findall函数返回字符串中所有匹配结果的正则表达式列表。

2、如果没有分组的正则是返回的正则匹配,分组返回的是分组匹配而非整个正则匹配。

实例

找到所有与pattern匹配的子串(不重叠),并将其放入列表。


import re
lst = re.findall("[1-9]\d*","qw21313h1o58p4kjh8123jkh8435u")
for x in lst:
   print(x,end=" ")

#输出结果:21313 1 58 4 8123 8435

实例扩展:

python3中函数说明:


findall(pattern, string, flags=0)
   Return a list of all non-overlapping matches in the string.

If one or more capturing groups are present in the pattern, return
   a list of groups; this will be a list of tuples if the pattern
   has more than one group.

Empty matches are included in the result.

两种形式的使用方法:


import re
kk = re.compile(r'\d+')
kk.findall('one1two2three3four4')
#[1,2,3,4]

#注意此处findall()的用法,可传两个参数;
kk = re.compile(r'\d+')
re.findall(kk,"one123")
#[1,2,3]

其中,含()时要注意:


import re

string="abcdefg  acbdgef  abcdgfe  cadbgfe"

#带括号与不带括号的区别
#不带括号
regex=re.compile("((\w+)\s+\w+)")
print(regex.findall(string))
#输出:[('abcdefg  acbdgef', 'abcdefg'), ('abcdgfe  cadbgfe', 'abcdgfe')]

regex1=re.compile("(\w+)\s+\w+")
print(regex1.findall(string))
#输出:['abcdefg', 'abcdgfe']

regex2=re.compile("\w+\s+\w+")
print(regex2.findall(string))
#输出:['abcdefg  acbdgef', 'abcdgfe  cadbgfe']

来源:https://www.py.cn/jishu/jichu/33358.html

标签:python,re.findall
0
投稿

猜你喜欢

  • 细化解析:MySQL 搜索中的大小写敏感性

    2008-11-27 15:53:00
  • Python实现对excel文件列表值进行统计的方法

    2022-08-08 17:42:23
  • PyCharm代码格式调整方法

    2021-05-21 14:59:32
  • python中的全局变量用法分析

    2022-04-04 01:29:02
  • 在Django中预防CSRF攻击的操作

    2023-11-11 15:55:13
  • Python利用FlashText算法实现替换字符串

    2023-05-17 06:18:11
  • 三大措施设置数据库安全 保障网站安全运营

    2008-11-28 14:41:00
  • python 性能提升的几种方法

    2022-05-21 06:38:18
  • Oracle数据库性能优化技术开发者网络Oracle

    2010-07-18 13:05:00
  • 利用ASP在线维护数据库

    2007-10-12 13:53:00
  • 浅谈python正则的常用方法 覆盖范围70%以上

    2022-05-18 21:01:13
  • Python实现的NN神经网络算法完整示例

    2023-10-06 04:30:41
  • Oracle数据库SQL语句性能调整的基本原则

    2009-03-25 16:55:00
  • Python解析json文件相关知识学习

    2022-04-01 12:58:19
  • Python中的迭代器与生成器使用及说明

    2022-01-01 08:14:27
  • JSP学生信息管理系统设计

    2023-07-13 03:37:30
  • python通过http上传文件思路详解

    2022-02-08 12:48:30
  • python实现KNN近邻算法

    2022-08-13 08:24:58
  • python 实现多维数组(array)排序

    2022-03-26 07:35:48
  • Python实现文件压缩和解压的示例代码

    2022-04-10 08:20:19
  • asp之家 网络编程 m.aspxhome.com