您好,登錄后才能下訂單哦!
怎么解析Python正則表達式,針對這個問題,這篇文章詳細介紹了相對應的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
今天給大家介紹一下,正則表達式在Python中是如何使用的。這樣說的原因是正則表達式并不是Python所獨有的,而是自成體系,在很多地方都有使用。而正則表達式在Python中主要是re模塊來實現(xiàn)的,所以學習Python正則表達式主要就是學習re模塊,然后需要熟悉正則表達式的語言,這樣基本就可以掌握了。
# re模塊
re模塊中常用的函數(shù)有
compile, findall,match,search,sub,split
compile函數(shù)的作用是編譯一個正則表達式模板,返回一個模板的對象(實例)。complie函數(shù)一般是配合findall來使用的,findall,的意思就是說有了一個模板的對象以后,用這個對象去匹配你想匹配的字符串,然后把匹配到的全部以列表形式返回。函數(shù)search也是用來在一個字符串中找模板對象所匹配到的字符,如果多次匹配成功只返回第一個。match函數(shù)只會匹配字符串的開頭,如果匹配失敗,則返回None。而sub函數(shù)的意思是替換的意思,split是分割,根據(jù)指定的字符分割字符串,而Python字符串分割的主要區(qū)別是可以選擇多個分割符,而Python字符串自帶的分割方法只能選擇一個分割符。下面寫個簡單的栗子,這里不用管為啥去匹配那個,只是個栗子,讓大家看看正則表達式的語法是怎么樣的。
這篇文章寫得比較簡單,之所以這樣是因為不想讓大家覺得正則表達式這塊的內(nèi)容很多,其實沒有想象的那么難,基本就這些東西,學會這個也就夠用了。文章沒有排版,主要作者太懶,看著不爽的話,可以看下后面的一個參考鏈接。
# - * - 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+", # 匹配所有數(shù)字,\d表示匹配數(shù)字,+號表示匹配前1個字符1次或者無限次
"abcd?", # 匹配abc和abcd,?號表示匹配前一個字符0次或者1次
"abcd*", # 星號*匹配前一個字符0次或者無數(shù)次
"\w", # 小寫w匹配單詞字符
"\W", # 匹配非單詞字符
"\d+\-\d+\-\d+", # 匹配以橫線分割的日期
"^a[bc]d?", # ^表示匹配字符串的開頭
"\|.", # \表示轉(zhuǎn)義字符,.表示匹配除換行符以外的字符
"\d", # 匹配數(shù)字
"\d+|\w+", # |表示匹配左右2個表達式任意一個
"\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)
"""
正則表達式就說到這里,入門以后主要還是要多練,然后結合具體的任務多嘗試,如果能掌握好的,會省事很多
"""
- - - - - - - - - - - - - - - - - - - -
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正則表達式問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關注億速云行業(yè)資訊頻道了解更多相關知識。
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。