Python 正则表达式(Google Python Course)
2016-10-10 09:33
501 查看
用了这么久正则表达式,一直是复制粘贴或求助论坛~期间也看着教程学过,可是一直领会不了啊~领会不了!看了Google For Education的Python Course终于知道点皮毛了
对于输出,search函数的输出可以用
重复是贪心的,它先找到第一个匹配的位置,然后找尽可能远的位置,如:
这并不会输出
Python函数
主要是两个函数:import re match = re.search(pattern, string, flags=0) match = re.findall(pattern, string, flags=0)
partten匹配模式一般以r开头,表示raw string
flags默认是False,表示不区分大小写
对于输出,search函数的输出可以用
match.group或
match.group(1)表示第一个分组;findall函数的输出是一个多元组。
匹配模式符号
符号 | 功能 |
---|---|
a, X, 9 | 原始字符串仅表示字符串本身 |
. | (点号)表示除\n外的任意单个字符 |
\w | 表示单个字符和下划线,即[a-zA-Z0-9_],可以理解成可以出现在“word”中的符号,不过不是指真正的一个单词 |
\W | 大写表示和小写相反的含义,即任意“非单词”字符 |
\b | 单词和非单词的边界,但是不表示任何字符,仅表示位置 |
\s | (space)表示一个空白符:空格、\n、\r、\t、\f |
\S | 大写表示非空白符 |
\d | (decimal)表示一个数字 |
^, $ | 起止符 |
\ | 转义符 |
/ | ? |
重复
符号 | 功能 |
---|---|
+ | 左边匹配模式出现了≥1次 |
* | 左边匹配模式出现了≥0次 |
? | 左边匹配模式出现了=0,1次 |
str = "<b>foo</b> and <i>so on</i>" match = re.search(r'<.*>', str) if match: print(match.group())
这并不会输出
<b>而是输出所有字符串,因为
*号会匹配到最远的位置,也就是中间的“b>foo….<.*?>
中括号
中括号表示字符集合,如:符号 | 功能 |
---|---|
[abc] | 表示a或b或c |
[\w.-] | 表示单词或-号 |
[^ab] | 表示除a、b外的所有字符串 |
分组记录
圆括号表示匹配的分组,以便后续输出str = 'purple alice-b@google.com monkey dishwasher' match = re.search('([\w.-]+)@([\w.-]+)', str) if match: print match.group() ## 'alice-b@google.com' (the whole match) print match.group(1) ## 'alice-b' (the username, group 1) print match.group(2) ## 'google.com' (the host, group 2)
str = 'purple alice@google.com, blah monkey bob@abc.com blah dishwasher' ## Here re.findall() returns a list of all the found email strings emails = re.findall(r'[\w\.-]+@[\w\.-]+', str) ## ['alice@google.com', 'bob@abc.com'] for email in emails: # do something with each found email string print email
str = 'purple alice@google.com, blah monkey bob@abc.com blah dishwasher' tuples = re.findall(r'([\w\.-]+)@([\w\.-]+)', str) print tuples ## [('alice', 'google.com'), ('bob', 'abc.com')] for tuple in tuples: print tuple[0] ## username print tuple[1] ## host
替换
用\1,
\2来表示替换位置
str = 'purple alice@google.com, blah monkey bob@abc.com blah dishwasher' ## re.sub(pat, replacement, str) -- returns new string with all replacements, ## \1 is group(1), \2 group(2) in the replacement print re.sub(r'([\w\.-]+)@([\w\.-]+)', r'\1@yo-yo-dyne.com', str) ## purple alice@yo-yo-dyne.com, blah monkey bob@yo-yo-dyne.com blah dishwasher
相关文章推荐
- 20121020 Google python课堂 正则表达式
- Google's Python Class(六)——Python 正则表达式
- Google Python Class 之——正则表达式提取html网页数据字段
- Python天天美味(15) - Python正则表达式操作指南(re使用)(转)
- 转载:Python正则表达式的用法(1. 匹配)
- 比较详细Python正则表达式操作指南(re使用)
- Python正则表达式(一)
- python 正则表达式 概述及常用字符
- 在python 中使用正则表达式
- Python 正则表达式操作指南
- Python 正则表达式操作指南
- Python正则表达式(三)
- 正则表达式在python中的应用
- c++正则表达式,python
- Python正则表达式操作指南
- python中正则表达式与re 模块
- Python正则表达式的几种匹配用法
- python中的一些正则表达式的含义
- 比较详细Python正则表达式操作指南
- Python正则表达式(二)