Python正则表达式符号与方法

导入re库文件

import re

from re import findall,search,S

secret_code = ‘hadkfalifexxIxxfasdjifja134xxlovexx23345sdfxxyouxx8dfse‘   #红色为待带抓取内容

.的使用举例

a = ‘xy123‘

b = re.findall(‘x...‘,a)

print b          #执行后返回xy12,x...中“.”代表占位符,几个点就取x后几位

*的使用举例

a = ‘xyxy123‘

b = re.findall(‘x*‘,a)

print b

#执行后返回[‘x‘, ‘‘, ‘x‘, ‘‘, ‘‘, ‘‘, ‘‘],x*代表生成匹配x(0-无穷次)的列表,不匹配的生成空字符串‘‘

?的使用举例

a = ‘xy123x‘

b = re.findall(‘x?‘,a)

print b

#执行后返回[‘x‘, ‘‘, ‘‘, ‘‘, ‘‘, ‘x‘, ‘‘],x?代表生成匹配x(0-无穷次)的列表,和x*的区别是列表最后加一个空字符串‘‘

‘‘‘上面的内容全部都是只需要了解,需要掌握的只有下面这一种组合方式(.*?)‘‘‘

#.*的使用举例

b = re.findall(‘xx.*xx‘,secret_code)

print b

# #.*?的使用举例

c = re.findall(‘xx.*?xx‘,secret_code)

print c

#使用括号与不使用括号的差别

d = re.findall(‘xx(.*?)xx‘,secret_code)

print d

for each in d:

print each

s = ‘‘‘sdfxxhello

xxfsdfxxworldxxasdf‘‘‘

d = re.findall(‘xx(.*?)xx‘,s,re.S)

print d

对比findall与search的区别

s2 = ‘asdfxxIxx123xxlovexxdfd‘

# f = re.search(‘xx(.*?)xx123xx(.*?)xx‘,s2).group(2)

# print f

f2 = re.findall(‘xx(.*?)xx123xx(.*?)xx‘,s2)

print f2[0][1]

sub的使用举例

s = ‘123rrrrr123‘

output = re.sub(‘123(.*?)123‘,‘123%d123‘%789,s)

print output

演示不同的导入方法

info = findall(‘xx(.*?)xx‘,secret_code,S)

for each in info:

print each

不要使用compile

pattern = ‘xx(.*?)xx‘

new_pattern = re.compile(pattern,re.S)

output = re.findall(new_pattern,secret_code)

print output

匹配数字

a = ‘asdfasf1234567fasd555fas‘

b = re.findall(‘(\d+)‘,a)

print b

时间: 2024-08-04 04:58:47

Python正则表达式符号与方法的相关文章

python正则表达式(3)--match方法

1.re.match函数 re.match 尝试从字符串的起始位置匹配一个模式,如果不是起始位置匹配成功的话,match()就返回none. (1)函数语法: re.match(pattern, string, flags=0) 函数参数说明: pattern   匹配的正则表达式    string      要匹配的字符串 flgs         标志位,用于控制正则表达式的匹配方式 我们可以使用group(num) 或 groups() 匹配对象函数来获取匹配表达式. group(num

Python正则表达式与split()方法一起使用

import re#split 只能实现单个字符串的分割string="guoshun is a good boy"print(string.split(' '))#但是如果中间又好几个空格 那么split耶只能分割一个空格string2="guoshun is good boy"#regex提供了split方法print(re.split("\s+",string2))#也可以同时使用多个分割符号string3="guoshun/is

python正则表达式(4)--search方法

摘自:https://www.cnblogs.com/zeke-python-road/p/9565419.html 1.re.search函数 re.search 扫描整个字符串并返回第一个成功的匹配,如果匹配失败search()就返回None. (1)函数语法: re.search(pattern, string, flags=0) 函数参数说明: pattern   匹配的正则表达式 string      要匹配的字符串 flgs         标志位,用于控制正则表达式的匹配方式 我

第十六章 Python正则表达式

正则表达式在每种语言中都会有,目的就是匹配符合你预期要求的字符串. Python正则表达式主要由re库提供,拥有了基本所有的表达式. 16.1 Python正则表达式 符号 描述 示例 . 匹配除换行符(\n)之外的任意单个字符 字符串123\n456,匹配123:1.3 ^ 匹配字符串开头 abc\nxyz,匹配以abc开头的行:^abc $ 匹配字符串结尾 abc\nxyz,匹配以xyz结束的行:xyz$ * 匹配多个 hello\nword,匹配以w开头d结尾的单词:w*d + 匹配1个或

python 正则表达式点号与'\n'符号的问题

遇到了一个小虫,特记录之. 1.正则表达式及英文的处理如下: >>> import re >>> b='adfasdfasf<1safadsaf>23wfsa<13131>' >>> pat = re.compile('<.*?>') >>> pat.findall(b) ['<1safadsaf>', '<13131>'] 2. 换成中文貌似就没反应了 >>&g

python正则表达式之re模块方法介绍

python正则表达式之re模块其他方法 1:search(pattern,string,flags=0) 在一个字符串中查找匹配 2:findall(pattern,string,flags=0) 找到匹配,返回所有匹配部分的列表 In [1]: import re In [2]: str1 = 'imoooc videonum = 1000' In [3]: str1.find('1000') Out[3]: 18 In [4]: info = re.search(r'\d+',str1)

Python正则表达式Regular Expression基本用法

资料来源:http://blog.csdn.net/whycadi/article/details/2011046   直接从网上资料转载过来,作为自己的参考.这个写的很清楚.先拿来看看. 1.正则表达式re模块的基本函数. (1)findall函数的用法 findall(rule,target[,flag])是在目标字符串中找到符合规则的字符串.参数说明:rule表示规则,target表示目标字符串,[,flag]表示的是规则选项.返回的结果是一个列表.若没找到符合的,是一个空列表. 如: 因

【Python基础学习篇十】Python正则表达式(2015-01-01)

一.正则表达式简介 正则表达式用于文本匹配的工具,在源字符串中查找与给定的正则表达式相匹配的部分.一个正则表达式是由字母.数字和特殊字符组成的.正则表达式中有许多特殊的字符,这些特殊字符是构成正则表达式的要素. 1.正则表达式中的特殊字符: 2.正则表达式中的常用限定符: 利用{}可以控制字符重复的次数. 例如,\d{1,4}表示1位到3位的数字: 某些地区的电话号码是8位数,区号也有可能是3位或4位数字. \d{3}-\d{8}|\d{4}-\d{7} 3.限定符与"?"的组合 二.

Python——正则表达式(5)

本文译自官方文档: Regular Expression HOWTO 参考文章: Python--正则表达式(1) Python--正则表达式(2) Python--正则表达式(3) Python--正则表达式(4) 全文下载: Python正则表达式基础 ====================================================================================== 6.常见问题 正则表达式在应用中是非常强大的工具,但有时候它们并不能