常用正则表达式

简介:

   下面的例子默认以python为实现语言,用到python的re模块。

0、正则表达式的文档。

(1)、正则表达式30分钟入门教程

(2)、另一个不错的入门教程

(3)、揭开正则表达式的神秘面纱,个人觉得这篇文章对Multiline的讲解特别到位,截图如下:

141130303.jpg

1、提取双引号及之间的内容。

(1)、用re.findall。

1
2
3
4
text  =  '''abc"def"ghi'''
re.findall(r '".+"' , text)
#结果
[ '"def"' ]

(2)、用re.search。

1
2
3
>>> text  =  '''abc"def"ghi'''
>>> re.search(r '"(.+)"' , text).group( 0 )
'"def"'

2、提取双引号之间的内容。规则: (pattern)

(1)、用re.findall。

1
2
3
4
text  =  '''abc"def"ghi'''
re.findall(r '"(.+)"' , text)
#结果
[ 'def' ]

1的区别是在需要返回的内容两边加上了括号。

(2)、用re.search。

1
2
3
>>> text  =  '''abc"def"ghi'''
>>> re.search(r '"(.+)"' , text).group( 1 )
'def'

3、效果同2。规则: (?<=pattern)(?=pattern)

1
2
3
4
text  =  '''abc"def"ghi'''
re.findall(r '(?<=").+(?=")' , text)
#结果
[ 'def' ]

4、C++中三种正则表达式比较(C regex,C ++regex,boost regex)

5、查找以某些字符串打头的行。比如查找以+++、---、index打头的行:

1
2
3
4
5
6
7
8
#方法一,按行匹配
for  in  lst:
     if  re.match(r "(---|\+\+\+|index).*" , i):
         print  i
#方法二,一次性匹配
re.findall(r '^(?:\+\+\+|---|index).*$' , content, re.M)
#方法二精简版
re.findall(r '^(?:[-\+]{3}|index).*$' , content, re.M)

6、包含/不包含(参考:利用正则表达式排除特定字符串

(0)、文本内容

1
2
3
4
5
6
7
8
9
10
11
12
>>>  print (text)
www.sina.com.cn
www.educ.org
www.hao.cc
www.baidu.com
www. 123.com
 
sina.com.cn
educ.org
hao.cc
baidu.com
123.com

(1)、匹配以www打头的行

1
2
>>> re.findall(r '^www.*$' , text, re.M)
[ 'www.sina.com.cn' 'www.educ.org' 'www.hao.cc' 'www.baidu.com' 'www.123.com' ]

(2)、匹配不以www打头的行

1
2
>>> re.findall(r '^(?!www).*$' , text, re.M)
[' ', ' sina.com.cn ', ' educ.org ', ' hao.cc ', ' baidu.com ', ' 123.com ']

(3)、匹配以cn结尾的行

1
2
>>> re.findall(r '^.*?cn$' , text, re.M)
[ 'www.sina.com.cn' 'sina.com.cn' ]

(4)、匹配不以com结尾的行

1
2
>>> re.findall(r '^.*?(?<!com)$' , text, re.M)
[ 'www.sina.com.cn' 'www.educ.org' 'www.hao.cc' , ' ', ' sina.com.cn ', ' educ.org ', ' hao.cc']

(5)、匹配包含com的行

1
2
>>> re.findall(r '^.*?com.*?$' , text, re.M)
[ 'www.sina.com.cn' 'www.baidu.com' 'www.123.com' 'sina.com.cn' 'baidu.com' '123.com' ]

(6)、匹配不包含com的行

1
2
3
4
5
>>> re.findall(r '^(?!.*com).*$' , text, re.M)
[ 'www.educ.org' 'www.hao.cc' , ' ', ' educ.org ', ' hao.cc']
 
>>> re.findall(r '^(?:(?!com).)*?$' , text, re.M)
[ 'www.educ.org' 'www.hao.cc' , ' ', ' educ.org ', ' hao.cc']

7、利用分组得到网址的第一级,即去除后面几级。(匹配全部,去除部分)

方法一:

1
2
3
>>> strr  =  'http://www.baidu.com/abc/d.html'
>>> re.findall(r '(http://.+?)/.*' , strr)
[ 'http://www.baidu.com' ]

方法二:

1
2
>>> re.sub(r '(http://.+?)/.*' , r '\1' , strr)
'http://www.baidu.com'

8、两个有助于理解正则分组的例子。

(1)、

1
2
3
4
5
6
7
8
9
>>> strr  =  'A/B/C'
>>> re.sub(r '(.)/(.)/(.)' , r 'xx' , strr)
'xx'
>>> re.sub(r '(.)/(.)/(.)' , r '\1xx' , strr)
'Axx'
>>> re.sub(r '(.)/(.)/(.)' , r '\2xx' , strr)
'Bxx'
>>> re.sub(r '(.)/(.)/(.)' , r '\3xx' , strr)
'Cxx'

(2)、

1
2
3
4
5
6
7
8
9
>>> text  =  'AA,BB:222'
>>> re.search(r '(.+),(.+):(\d+)' , text).group( 0 )
'AA,BB:222'
>>> re.search(r '(.+),(.+):(\d+)' , text).group( 1 )
'AA'
>>> re.search(r '(.+),(.+):(\d+)' , text).group( 2 )
'BB'
>>> re.search(r '(.+),(.+):(\d+)' , text).group( 3 )
'222'

9、提取含有hello字符串的div。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
>>> content
'<div id="abc"><div id="hello1"><div id="def"><div id="hello2"><div id="hij">'
>>> 
>>> p  =  r '<div((?!div).)+hello.+?>'
>>> re.search(p, content).group()
'<div id="hello1">'
>>> re.findall(p, content)
[ '"', '"' ]
>>>  for  iter  in  re.finditer(p, content):
     print ( iter .group())
 
<div  id = "hello1" >
<div  id = "hello2" >
>>> 
>>> p  =  r '<div[^>]+hello.+?>'
>>> re.search(p, content).group()
'<div id="hello1">'
>>> re.findall(p, content)
[ '<div id="hello1">' '<div id="hello2">' ]
>>>  for  iter  in  re.finditer(p, content):
     print ( iter .group())
 
<div  id = "hello1" >
<div  id = "hello2" >

10、据walker猜测:在python3的Unicode字符集下,\s匹配\f\n\r\t\v加全角半角空格,共7个字符。

12、如果所使用的工具支持肯定环视(positive lookahead),同时可以在肯定环视中使用捕获括号(capturing parentheses),就能模拟实现固化分组(atomic grouping)和占有优先量词(possessive quantifiers)。

13、千分位。

(1)、Python

1
2
3
4
5
>>>  format ( 23456789 ',' )
'23,456,789'
# 利用肯定逆序环视与肯定顺序环视
>>> re.sub(r '(?<=\d)(?=(?:\d{3})+$)' ',' '2345678' )
'2,345,678'

(2)、JavaScript

1
2
3
//利用肯定顺序环视(因为js不支持肯定逆序环视)
//结果为"23,456,789"
"23456789" .replace(/(\d)(?=(?:\d{3})+$)/g,  "$1," )


相关阅读:

1、Python中的re.search和re.findall之间的区别和联系

2、正则表达式测试:regex101regexr

3、Python的regex模块——更强大的正则表达式引擎

4、桌面工具:RegexBuddy

4.1、修改注册表:RegexBuddy 4.7.0 x64 评估试用到期,无限试用的办法


*** walker * Updated 2014-12-09 ***

本文转自walker snapshot博客51CTO博客,原文链接http://blog.51cto.com/walkerqt/1246206如需转载请自行联系原作者


RQSLT

相关文章
|
1天前
|
Java
正则表达式
正则表达式
|
1月前
最全面的常用正则表达式大全
最全面的常用正则表达式大全
14 1
|
1月前
|
JavaScript 前端开发 Java
正则表达式详细讲解
正则表达式详细讲解
正则表达式详细讲解
|
1月前
使用正则表达式
使用正则表达式。
18 1
|
7月前
|
机器学习/深度学习 程序员 vr&ar
正则表达式的使用
正则表达式的使用
36 0
|
8月前
|
C++
C++正则表达式
C++正则表达式
|
数据采集 机器学习/深度学习 移动开发
我学会了,正则表达式
爬虫是**非常的**的强大,相信不少朋友都有所耳闻,它帮助我们更快地“获得”我们所要关键数据。那么,它怎么知道我们要需要什么内容?它又是如何工作的?在这篇文章里,我们一起来看看。
76 0
我学会了,正则表达式
|
数据安全/隐私保护
常用的正则表达式
常用的正则表达式
|
Windows
正则表达式汇总
常用正则表达式
161 0
|
移动开发
【小技巧】正则表达式
正则表达式小技巧 1 空行替换 ^\r\n
496 0