前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >[编程经验] Python正则表达式

[编程经验] Python正则表达式

作者头像
用户1622570
发布2018-04-11 16:11:17
7310
发布2018-04-11 16:11:17
举报

Hello,大家好。又见面了,今天给大家介绍一下,正则表达式在Python中是如何使用的。这样说的原因是正则表达式并不是Python所独有的,而是自成体系,在很多地方都有使用。而正则表达式在Python中主要是re模块来实现的,所以学习Python正则表达式主要就是学习re模块,然后需要熟悉正则表达式的语言,这样基本就可以掌握了。

# re模块

re模块中常用的函数有

compile, findall,match,search,sub,split

compile函数的作用是编译一个正则表达式模板,返回一个模板的对象(实例)。complie函数一般是配合findall来使用的,findall,的意思就是说有了一个模板的对象以后,用这个对象去匹配你想匹配的字符串,然后把匹配到的全部以列表形式返回。函数search也是用来在一个字符串中找模板对象所匹配到的字符,如果多次匹配成功只返回第一个。match函数只会匹配字符串的开头,如果匹配失败,则返回None。而sub函数的意思是替换的意思,split是分割,根据指定的字符分割字符串,而Python字符串分割的主要区别是可以选择多个分割符,而Python字符串自带的分割方法只能选择一个分割符。下面写个简单的栗子,这里不用管为啥去匹配那个,只是个栗子,让大家看看正则表达式的语法是怎么样的。

这篇文章写得比较简单,之所以这样是因为不想让大家觉得正则表达式这块的内容很多,其实没有想象的那么难,基本就这些东西,学会这个也就够用了。文章没有排版,主要作者太懒,看着不爽的话,可以看下后面的一个参考链接。

代码语言:python
复制
# - * - coding:utf-8 - * -
import re
# complie, findall, search, match, sub, split
test_string = 'abc|2abcd)3Hello world4abcddd,\n 2017-11-19|Python正则表达式详解'
patterns = [
            "Hello world", # 匹配Hello world
            "\d+", # 匹配所有数字,\d表示匹配数字,+号表示匹配前1个字符1次或者无限次
            "abcd?", # 匹配abc和abcd,?号表示匹配前一个字符0次或者1次
            "abcd*", # 星号*匹配前一个字符0次或者无数次
            "\w", # 小写w匹配单词字符
            "\W",  # 匹配非单词字符
            "\d+\-\d+\-\d+", # 匹配以横线分割的日期
            "^a[bc]d?", # ^表示匹配字符串的开头
            "\|.", # \表示转义字符,.表示匹配除换行符以外的字符
            "\d", # 匹配数字
            "\d+|\w+", # |表示匹配左右2个表达式任意一个
            "\s", # 匹配空白字符
            "\s\w+", # 匹配空白字符后跟着的单词字符
            ".", # 匹配除换行符以外的字符
            "\D",  # 匹配非数字字符
            "\S", # 大写S匹配任意非空字符
            ]
for pattern in patterns:
    print "- "*20
    print("current regex is :  {}".format(pattern))
    p = re.compile(pattern).findall(test_string)
    if p:
        print("findall results is:  {}".format(p))
    else:
        print("[!] match failed ")
    p1 = re.search(pattern, test_string, flags=0)
    if p1:
        result = p1.group()
        print("search results is  :{}".format(result))
    else:
        print("[!] match failed ")
    p2 = re.match(pattern, test_string, flags=0)
    if p2:
        result = p1.group()
        print("match results is  :{}".format(result))
    else:
        print("[!] match failed ")
print("- "*20)
test_string = '2017,11,19'
re_sub = re.sub(",", ":", test_string)
print(re_sub)
print("- "*20)
test_string = "2017,11,19"
re_split = re.split(",", test_string)
print(re_split)
"""

正则表达式就说到这里,入门以后主要还是要多练,然后结合具体的任务多尝试,如果能掌握好的,会省事很多

代码语言:python
复制
"""
- - - - - - - - - - - - - - - - - - - - 
current regex is :  Hello world
findall results is:  ['Hello world']
search results is  :Hello world
[!] match failed 
- - - - - - - - - - - - - - - - - - - - 
current regex is :  \d+
findall results is:  ['2', '3', '4', '2017', '11', '19']
search results is  :2
[!] match failed 
- - - - - - - - - - - - - - - - - - - - 
current regex is :  abcd?
findall results is:  ['abc', 'abcd', 'abcd']
search results is  :abc
match results is  :abc
- - - - - - - - - - - - - - - - - - - - 
current regex is :  abcd*
findall results is:  ['abc', 'abcd', 'abcddd']
search results is  :abc
match results is  :abc
- - - - - - - - - - - - - - - - - - - - 
current regex is :  \w
findall results is:  ['a', 'b', 'c', '2', 'a', 'b', 'c', 'd', '3', 'H', 'e', 'l', 'l', 'o', 'w', 'o', 'r', 'l', 'd', '4', 'a', 'b', 'c', 'd', 'd', 'd', '2', '0', '1', '7', '1', '1', '1', '9', 'P', 'y', 't', 'h', 'o', 'n', '正', '则', '表', '达', '式', '详', '解']
search results is  :a
match results is  :a
- - - - - - - - - - - - - - - - - - - - 
current regex is :  \W
findall results is:  ['|', ')', ' ', ',', '\n', ' ', '-', '-', '|']
search results is  :|
[!] match failed 
- - - - - - - - - - - - - - - - - - - - 
current regex is :  \d+\-\d+\-\d+
findall results is:  ['2017-11-19']
search results is  :2017-11-19
[!] match failed 
- - - - - - - - - - - - - - - - - - - - 
current regex is :  ^a[bc]d?
findall results is:  ['ab']
search results is  :ab
match results is  :ab
- - - - - - - - - - - - - - - - - - - - 
current regex is :  \|.
findall results is:  ['|2', '|P']
search results is  :|2
[!] match failed 
- - - - - - - - - - - - - - - - - - - - 
current regex is :  \d
findall results is:  ['2', '3', '4', '2', '0', '1', '7', '1', '1', '1', '9']
search results is  :2
[!] match failed 
- - - - - - - - - - - - - - - - - - - - 
current regex is :  \d+|\w+
findall results is:  ['abc', '2', 'abcd', '3', 'Hello', 'world4abcddd', '2017', '11', '19', 'Python正则表达式详解']
search results is  :abc
match results is  :abc
- - - - - - - - - - - - - - - - - - - - 
current regex is :  \s
findall results is:  [' ', '\n', ' ']
search results is  : 
[!] match failed 
- - - - - - - - - - - - - - - - - - - - 
current regex is :  \s\w+
findall results is:  [' world4abcddd', ' 2017']
search results is  : world4abcddd
[!] match failed 
- - - - - - - - - - - - - - - - - - - - 
current regex is :  .
findall results is:  ['a', 'b', 'c', '|', '2', 'a', 'b', 'c', 'd', ')', '3', 'H', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', '4', 'a', 'b', 'c', 'd', 'd', 'd', ',', ' ', '2', '0', '1', '7', '-', '1', '1', '-', '1', '9', '|', 'P', 'y', 't', 'h', 'o', 'n', '正', '则', '表', '达', '式', '详', '解']
search results is  :a
match results is  :a
- - - - - - - - - - - - - - - - - - - - 
current regex is :  \D
findall results is:  ['a', 'b', 'c', '|', 'a', 'b', 'c', 'd', ')', 'H', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', 'a', 'b', 'c', 'd', 'd', 'd', ',', '\n', ' ', '-', '-', '|', 'P', 'y', 't', 'h', 'o', 'n', '正', '则', '表', '达', '式', '详', '解']
search results is  :a
match results is  :a
- - - - - - - - - - - - - - - - - - - - 
current regex is :  \S
findall results is:  ['a', 'b', 'c', '|', '2', 'a', 'b', 'c', 'd', ')', '3', 'H', 'e', 'l', 'l', 'o', 'w', 'o', 'r', 'l', 'd', '4', 'a', 'b', 'c', 'd', 'd', 'd', ',', '2', '0', '1', '7', '-', '1', '1', '-', '1', '9', '|', 'P', 'y', 't', 'h', 'o', 'n', '正', '则', '表', '达', '式', '详', '解']
search results is  :a
match results is  :a
- - - - - - - - - - - - - - - - - - - - 
2017:11:19
- - - - - - - - - - - - - - - - - - - - 

['2017', '11', '19']

参考:

http://www.cnblogs.com/huxi/archive/2010/07/04/1771073.html

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2017-11-19,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 机器学习和数学 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档