怎么解析Python正則表達(dá)式,針對(duì)這個(gè)問題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問題的小伙伴找到更簡單易行的方法。
目前成都創(chuàng)新互聯(lián)公司已為上千余家的企業(yè)提供了網(wǎng)站建設(shè)、域名、網(wǎng)站空間、網(wǎng)站運(yùn)營、企業(yè)網(wǎng)站設(shè)計(jì)、麻城網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長,共同發(fā)展。
今天給大家介紹一下,正則表達(dá)式在Python中是如何使用的。這樣說的原因是正則表達(dá)式并不是Python所獨(dú)有的,而是自成體系,在很多地方都有使用。而正則表達(dá)式在Python中主要是re模塊來實(shí)現(xiàn)的,所以學(xué)習(xí)Python正則表達(dá)式主要就是學(xué)習(xí)re模塊,然后需要熟悉正則表達(dá)式的語言,這樣基本就可以掌握了。
# re模塊
re模塊中常用的函數(shù)有
compile, findall,match,search,sub,split
compile函數(shù)的作用是編譯一個(gè)正則表達(dá)式模板,返回一個(gè)模板的對(duì)象(實(shí)例)。complie函數(shù)一般是配合findall來使用的,findall,的意思就是說有了一個(gè)模板的對(duì)象以后,用這個(gè)對(duì)象去匹配你想匹配的字符串,然后把匹配到的全部以列表形式返回。函數(shù)search也是用來在一個(gè)字符串中找模板對(duì)象所匹配到的字符,如果多次匹配成功只返回第一個(gè)。match函數(shù)只會(huì)匹配字符串的開頭,如果匹配失敗,則返回None。而sub函數(shù)的意思是替換的意思,split是分割,根據(jù)指定的字符分割字符串,而Python字符串分割的主要區(qū)別是可以選擇多個(gè)分割符,而Python字符串自帶的分割方法只能選擇一個(gè)分割符。下面寫個(gè)簡單的栗子,這里不用管為啥去匹配那個(gè),只是個(gè)栗子,讓大家看看正則表達(dá)式的語法是怎么樣的。
這篇文章寫得比較簡單,之所以這樣是因?yàn)椴幌胱尨蠹矣X得正則表達(dá)式這塊的內(nèi)容很多,其實(shí)沒有想象的那么難,基本就這些東西,學(xué)會(huì)這個(gè)也就夠用了。文章沒有排版,主要作者太懶,看著不爽的話,可以看下后面的一個(gè)參考鏈接。
# - * - coding:utf-8 - * -
import re
# complie, findall, search, match, sub, split
test_string = 'abc|2abcd)3Hello world4abcddd,\n 2017-11-19|Python正則表達(dá)式詳解'
patterns = [
"Hello world", # 匹配Hello world
"\d+", # 匹配所有數(shù)字,\d表示匹配數(shù)字,+號(hào)表示匹配前1個(gè)字符1次或者無限次
"abcd?", # 匹配abc和abcd,?號(hào)表示匹配前一個(gè)字符0次或者1次
"abcd*", # 星號(hào)*匹配前一個(gè)字符0次或者無數(shù)次
"\w", # 小寫w匹配單詞字符
"\W", # 匹配非單詞字符
"\d+\-\d+\-\d+", # 匹配以橫線分割的日期
"^a[bc]d?", # ^表示匹配字符串的開頭
"\|.", # \表示轉(zhuǎn)義字符,.表示匹配除換行符以外的字符
"\d", # 匹配數(shù)字
"\d+|\w+", # |表示匹配左右2個(gè)表達(dá)式任意一個(gè)
"\s", # 匹配空白字符
"\s\w+", # 匹配空白字符后跟著的單詞字符
".", # 匹配除換行符以外的字符
"\D", # 匹配非數(shù)字字符
"\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)
"""
正則表達(dá)式就說到這里,入門以后主要還是要多練,然后結(jié)合具體的任務(wù)多嘗試,如果能掌握好的,會(huì)省事很多
"""
- - - - - - - - - - - - - - - - - - - -
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', '正', '則', '表', '達(dá)', '式', '詳', '解']
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正則表達(dá)式詳解']
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', '正', '則', '表', '達(dá)', '式', '詳', '解']
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', '正', '則', '表', '達(dá)', '式', '詳', '解']
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', '正', '則', '表', '達(dá)', '式', '詳', '解']
search results is :a
match results is :a
- - - - - - - - - - - - - - - - - - - -
2017:11:19
- - - - - - - - - - - - - - - - - - - -
['2017', '11', '19']
關(guān)于怎么解析Python正則表達(dá)式問題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關(guān)知識(shí)。
當(dāng)前標(biāo)題:怎么解析Python正則表達(dá)式
URL地址:http://aaarwkj.com/article32/iggspc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供做網(wǎng)站、軟件開發(fā)、微信小程序、外貿(mào)網(wǎng)站建設(shè)、網(wǎng)站維護(hù)、網(wǎng)站改版
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)