正则表达式就是用查找字符串的,它能查找规则比较复杂的字符串
反斜杠:正则表达式里面用"\"作为转义字符。
1 s='<a class="h3" href=""><b>python学习笔记</b></a>'
2
3 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)
1 def match(pattern, string, flags=0):
2 """Try to apply the pattern at the start of the string, returning
3 a match object, or None if no match was found."""
4 return _compile(pattern, flags).match(string)

函数参数说明:

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

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

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

2、re.search()函数

函数语法:

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

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

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

3、re.sub()函数

函数语法:

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

函数参数说明:

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

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

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

4、re.findall()函数

函数语法:

1 re.findall(pattern,string,flags)
1 def findall(pattern, string, flags=0):
2 """Return a list of all non-overlapping matches in the string.
3
4 If one or more capturing groups are present in the pattern, return
5 a list of groups; this will be a list of tuples if the pattern
6 has more than one group.
7
8 Empty matches are included in the result."""
9 return _compile(pattern, flags).findall(string)

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

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

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

更多相关文章

  1. Python3语法——Python3函数参数的各种形式
  2. python 中 字符串转换为数组,字典或表达式
  3. python 函数、参数及参数解构
  4. Python多行正则表达式忽略字符串中的n行
  5. python函数小练习
  6. Python学习总结-(15)---返回函数和闭包初步理解
  7. 初识python:高阶函数(附-高阶函数)
  8. Python学习札记(二十六) 函数式编程7 修饰器
  9. Python:运算类内建函数列举

随机推荐

  1. Android发展史(Android各版本特性-知识篇)
  2. Android简明开发教程二十四篇及示例代码
  3. android:visibility中"invisible"与"gone
  4. activity的android:name类名的简写方式
  5. 从头到尾给你讲明白Android(安卓)View实
  6. Android解析ClassLoader(二)Android中的Cla
  7. 【Android 开发】: Android 消息处理机制
  8. Android的NDK开发步骤
  9. Android音乐播放器汇总贴
  10. Android 去掉 Launcher3 里面默认的Googl