正则表达式就是用查找字符串的,它能查找规则比较复杂的字符串
反斜杠:正则表达式里面用"\"作为转义字符。
 s='<a class="h3" href=""><b>python学习笔记</b></a>'

 print(re.findall(r'\<a class\=\"h3\" href\=\"\"><b>(.*)\<\/b\>\<\/a\>',s))
里面的一个 r 表示字符串为非转义的原始字符串,让编译器忽略反斜杠,也就是忽略转义字符。但是这个字符串里没有反斜杠,所以这个 r 可有可无。

常用的功能函数包括:match、search、findall、sub
1、re.match()函数
函数语法:
re.match(pattern, string, flags=0)
 def match(pattern, string, flags=0):
"""Try to apply the pattern at the start of the string, returning
a match object, or None if no match was found."""
return _compile(pattern, flags).match(string)

函数参数说明:

  • pattern:匹配的正则表达式
  • string:要匹配的字符串
  • flag:标志位,用于控制正则表达式的匹配方式(是否匹配大小写、多行匹配等)

作用:match()函数只在字符串的开始位置尝试匹配正则表达式,即从位置0开始匹配。如果匹配成功,则返回一个匹配的对象;如果字符串开始不符合正则表达式,则匹配失败,函数返回None。

 import  re
test = 'http://news.163.com/17/0624/10/CNMHVBJP0001899N.html'
print(re.match(r'http',test)) # <_sre.SRE_Match object; span=(0, 4), match='http'>
print(re.match(r'news',test)) # None

2、re.search()函数

函数语法:

 re.search(pattern, string[, flags])
 def search(pattern, string, flags=0):
"""Scan through string looking for a match to the pattern, returning
a match object, or None if no match was found."""
return _compile(pattern, flags).search(string)

re.search()匹配整个字符串,直到找到第一个匹配的,如果字符串中没有匹配的,则返回None。

 import  re
test = 'I am a loving child to learn.'
print(re.search(r'I',test)) # <_sre.SRE_Match object; span=(0, 1), match='I'>
print(re.search(r'learn',test)) # <_sre.SRE_Match object; span=(23, 28), match='learn'>
print(re.search(r'alina',test)) # None

3、re.sub()函数

函数语法:

 re.sub(pattern,repl,string,count,flags)
 def sub(pattern, repl, string, count=0, flags=0):
"""Return the string obtained by replacing the leftmost
non-overlapping occurrences of the pattern in string by the
replacement repl. repl can be either a string or a callable;
if a string, backslash escapes in it are processed. If it is
a callable, it's passed the match object and must return
a replacement string to be used."""
return _compile(pattern, flags).sub(repl, string, count)

函数参数说明:

  • pattern:匹配的正则表达式
  • repl:替换的字符串
  • String:要被查找替换的原始字符串
  • count:匹配后替换的最大次数,默认0表示途欢所有的匹配

re.sub()函数用于替换字符串中的匹配项。

 import re
test = 'I am a loving child to learn.'
print(re.sub(r'child','MMMMM',test)) # 替换字符串,将child 替换成MMMMM

4、re.findall()函数

函数语法:

 re.findall(pattern,string,flags)
 def 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."""
return _compile(pattern, flags).findall(string)

re.findall()可以获取字符串中所有匹配的字符串

 import re
test = '<a href="http://www.educity.cn/zhibo/" target="_blank">直播课堂</a>'
print(re.findall(r'<a href="(.*)" target="_blank">(.*)</a>',test)) #[('http://www.educity.cn/zhibo/', '直播课堂')]

在练习正则表达式的时候,用的最多的就是re.findall()函数

Python_常用的正则表达式处理函数的更多相关文章

  1. Python常用的正则表达式处理函数

    Python常用的正则表达式处理函数 正则表达式是一个特殊的字符序列,用于简洁表达一组字符串特征,检查一个字符串是否与某种模式匹配,使用起来十分方便. 在Python中,我们通过调用re库来使用re模 ...

  2. php中常用的正则表达式函数

    php中常用的正则表达式函数 * preg_match() * preg_match_all() * preg_replace() * preg_filter() * preg_grep() * pr ...

  3. C#开发学习——常用的正则表达式

    对于想学习正则表达式的童鞋,一些基础的语法啥的,可以参考 http://www.cnblogs.com/China3S/archive/2013/11/30/3451971.html 下边是一些我们常 ...

  4. php中常用的字符串查找函数strstr()、strpos()实例解释

    string strstr ( string $haystack , mixed $needle [, bool $before_needle = false ] ) 1.$haystack被查找的字 ...

  5. PHP开发者常用的正则表达式及实例【长期更新收录】

    正则表达式在程序开发中是非常有用的,用好正则我们可以搜索.验证及替换文本或任何类型的字符.在这篇文章中,UncleToo为大家搜集了15个开发过程中常用的PHP正则表达式.函数及PHP示例,学习这些你 ...

  6. 常用js正则表达式大全

    常用js正则表达式大全.一.校验数字的js正则表达式 1 数字:^[0-9]*$ 2 n位的数字:^\d{n}$ 3 至少n位的数字:^\d{n,}$ 4 m-n位的数字:^\d{m,n}$ 5 零和 ...

  7. php 正则表达式一.函数解析

    php正则表达式官方手册参考....... 一.php中 常用的正则表达式函数 1.preg_match与preg_match_all preg_match: 函数信息 preg_match_all: ...

  8. php中的PCRE 函数,正则表达式处理函数。

    有时候在一些特定的业务场景中需要匹配,或者提取一些关键的信息,例如匹配网页中的一些链接, 提取一些数据时,可能会用到正则匹配. 下面介绍一下php中的一些常用的正则处理函数. 一.preg_repla ...

  9. js中常用的正则表达式

    我一般对正则的使用方式如下,该方法会返回一个boolean值,然后对这个返回值来进行判断 // 判断是否是整数 function isInt(num) { var reg = new RegExp(& ...

随机推荐

  1. 圆点博士 陀螺仪和加速度计MPU6050的单位换算方法

    圆点博士陀螺仪和加速度计MPU6050的单位换算方法 陀螺仪和加速度计MPU6050的单位换算方法 对于四轴的初学者,可能无法理解四轴源代码里面陀螺仪和加速度数据的那些数学转换方法.下面我们来具体描述 ...

  2. HDU 6141 I am your Father!(最小树形图+权值编码)

    http://acm.hdu.edu.cn/showproblem.php?pid=6141 题意: 求最大树形图. 思路: 把边的权值变为负值,那么这就是个最小树形图了,直接套模板就可以解决. 有个 ...

  3. zeptojs库解读3之ajax模块

    对于ajax,三步骤,第一,创建xhr对象:第二,发送请求:第三,处理响应. 但在编写过程中,实际中会碰到以下问题, 1.超时 2.跨域 3.后退 解决方法: 1.超时 设置定时器,规定的时间内未返回 ...

  4. hdu 5524 Subtrees dfs

    Subtrees Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Probl ...

  5. 关于怎么解决java.lang.NoClassDefFoundError错误

    五一在部署新的统一登录时,遇到这样一个问题: 很容易把java.lang.NoClassDefFoundError和java.lang.ClassNotfoundException这两个错误搞混,事实 ...

  6. ubuntu server 无线网卡的处理

    1) iwconfig 确定一下接口的名称 2) 编辑 /etc/network/interfaces 加入下面的代码 auto wlan0 iface wlan0 inet dhcp wpa-ssi ...

  7. Ubuntu 的 desktop 和 server 还是有区别。

    除了安装的包,比如 GUI, LAMP 上有差别之外,所用的内核也稍有不一样. 不过desktop可以通过安装 sudo apt-get install linux-image-server 之后,编 ...

  8. SpringBoot读取war包jar包Resource资源文件解决办法

    SpringBoot读取war包jar包Resource资源文件解决办法 场景描述 在开发过程中我们经常会碰到要在代码中获取资源文件的情况,而我在最近在SpringBoot项目中时碰到一个问题,就是在 ...

  9. Win10安装Mysql5.7数据库

    Win10安装Mysql5.7数据库 最近做个demo在自己本地装了一个mysql5.7,有些小麻烦记录一下. 安装环境:系统是 windows 10 1.官网下载 下载地址:https://dev. ...

  10. jsp动作之 setProperty

    setProperty用来设置useBean实例的属性. 如useBean实例化了一个类,类中有nickname属性,那么,我们可以用setProperty来重新定义他的值. setProperty有 ...