温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

怎么解析Python正则表达式

发布时间:2021-12-04 19:15:23 来源:亿速云 阅读:117 作者:柒染 栏目:大数据

怎么解析Python正则表达式,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

今天给大家介绍一下,正则表达式在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字符串自带的分割方法只能选择一个分割符。下面写个简单的栗子,这里不用管为啥去匹配那个,只是个栗子,让大家看看正则表达式的语法是怎么样的。

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

# - * - 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)

"""

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

"""

- - - - - - - - - - - - - - - - - - - - 

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']

关于怎么解析Python正则表达式问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI