1. 程式人生 > >Python基礎day-13[模塊:re未完]

Python基礎day-13[模塊:re未完]

import -s 數字 沒有 ret int bcs 意義 sdl

re:

  本質上就是一種小型語言。

  操作的對象是字符串。

re.findall():返回的是一個列表。匹配出所有符合條件的元素。

re.search():返回的是一個對象。只匹配出找到的第一個元素,如果匹配成功那麽返回的對象就是含匹配目標的索引範圍,和匹配內容。匹配失敗就返回None。

re.match():返回跟search一樣也是一個對象,只匹配文件開頭位置。如果匹配成功那麽返回的對象就是含匹配目標的索引範圍,和匹配內容。匹配失敗就返回None。

示例:
s = abclaskdjaklsjdlasdjabcasd123lksdlasd0asdasdaaa

res = re.match(
abc,s) print(res,res.group()) res = re.search(aaa,s) print(res,res.group()) res = re.findall(abc,s) print(res) 執行結果: D:\Python\Python36-32\python.exe E:/Python/DAY-14/day14_練習.py <_sre.SRE_Match object; span=(0, 3), match=abc> abc <_sre.SRE_Match object; span=(44, 47), match=aaa> aaa [
abc, abc] Process finished with exit code 0

元字符:

.:可以指代除換行符以外的任意符號。

import re

s = abclaskdjaklabcsjdlasdjaba1ccasd123a/clksdlasd0asdasdaaa

res = re.findall(a.c,s)
print(res)
res = re.findall(a..c,s)
print(res)

執行結果:
D:\Python\Python36-32\python.exe E:/Python/DAY-14/day14_練習.py
[abc, abc
, a1c, a/c] [a1cc] Process finished with exit code 0

^:只匹配字符串的開頭。

import re

s = abc1231321abclskdjlsk654sdsd3a13213a
s1 = 32132abc

res = re.findall(^abc,s)
print(res)
res = re.findall(^abc,s1)
print(res)

執行結果:
D:\Python\Python36-32\python.exe E:/Python/DAY-14/day14_練習.py
[abc]
[]

Process finished with exit code 0

$:只匹配字符串的結尾。

import re

s = abc1231321abclskdjlsk654sdsd3a13213a
s1 = 32133abc

res = re.findall(3a$,s)
print(res)
res = re.findall(3a$,s1)
print(res)

執行結果:
D:\Python\Python36-32\python.exe E:/Python/DAY-14/day14_練習.py
[3a]
[]

Process finished with exit code 0

*:符號前一個字符重復0-無窮次。

import re

s = abccccccabccdllkjabc

res = re.findall(abc*,s)
print(res)


執行結果:
D:\Python\Python36-32\python.exe E:/Python/DAY-14/day14_練習.py
[abcccccc, abcc, abc]

Process finished with exit code 0

+:符號前一個字符重復1-無窮次。

import re

s = abccccccabccdllkjabcabababc

res = re.findall(abc+,s)
print(res)


執行結果:
D:\Python\Python36-32\python.exe E:/Python/DAY-14/day14_練習.py
[abcccccc, abcc, abc, abc]

Process finished with exit code 0

?:符號前一個字符重復0-1次。

import re

s = abccccccabccdllkjabcabababc

res = re.findall(abc?,s)
print(res)


執行結果:
D:\Python\Python36-32\python.exe E:/Python/DAY-14/day14_練習.py
[abc, abc, abc, ab, ab, abc]

Process finished with exit code 0

{}:符號前一個字符指定重復的次數。

import re

s = abccccccabccdllkjabcccabababc

res = re.findall(abc{3,4},s)
print(res)
res = re.findall(abc{5},s)
print(res)


執行結果:
D:\Python\Python36-32\python.exe E:/Python/DAY-14/day14_練習.py
[abcccc, abccc]
[abccccc]

Process finished with exit code 0

\:轉義符,轉換後面字符的原本意思。

  轉義符跟元字符在一起的時候會去除其功能。還可以跟特殊的普通符號,把某些普通的符號轉成特別的意義。

可轉換的普通符號:

\d  匹配任何十進制數;      它相當於類 [0-9]
\D  匹配任何非數字字符;    它相當於類 [^0-9]
\s  匹配任何空白字符;      它相當於類 [ \t\n\r\f\v]
\S  匹配任何非空白字符;    它相當於類 [^ \t\n\r\f\v]
\w  匹配任何字母數字字符;   它相當於類 [a-zA-Z0-9_]
\W  匹配任何非字母數字字符; 它相當於類 [^a-zA-Z0-9_]
\b  匹配一個特殊字符邊界,比如空格 ,&,#等

():分組。把某些內容固定成一項。

import re
s = rabhdg8s99d222a1
ret=re.findall((ab)|\d+,s)
print(ret)

執行結果:
D:\Python\Python36-32\python.exe E:/Python/DAY-14/day14_練習.py
[ab, ‘‘, ‘‘, ‘‘, ‘‘]

Process finished with exit code 0

():還有個優先顯示功能,所以上面數字沒有顯示出來。
通過下列辦法解決:
import re
s = rabhdg8s99d222a1
ret=re.findall((?:ab)|\d+,s)
print(ret)

執行結果:
D:\Python\Python36-32\python.exe E:/Python/DAY-14/day14_練習.py
[ab, 8, 99, 222, 1]

Process finished with exit code 0

有名分組:

import re
s = rabhdg8s99d222a1
ret=re.search((?P<test>ab),s)
print(ret.group(test))

執行結果:
D:\Python\Python36-32\python.exe E:/Python/DAY-14/day14_練習.py
ab

Process finished with exit code 0

[]:字符集(多選一)只匹配一個字符集中的一個字符。

  在字符集中 只有 ^ \ - 還有自己的特殊意義,其他的都是普通的字符集,在字符集中的^號不是代表匹配開頭而是取反的意思。

import re
s = sdnaskdabcaspdlabefla1231ksaaabbcpas213lsdcbcablkc

res = re.findall(ab[ce],s)
print(res)
res = re.findall([a-z]+,s)
print(res)
res = re.findall([0-9]+,s)
print(res)
res = re.findall([^a-z]+,s)
print(res)
res = re.findall([\d]+,s)
print(res)

執行結果:
D:\Python\Python36-32\python.exe E:/Python/DAY-14/day14_練習.py
[abc, abe]
[sdnaskdabcaspdlabefla, ksaaabbcpas, lsdcbcablkc]
[1231, 213]
[1231, 213]
[1231, 213]

Process finished with exit code 0

|:或的意思。

import re
s = sdnaskdabcaspdlabefla1231ksaaabbcpas213lsdcbcablkc

res = re.findall(a.c|\d+,s)
print(res)

執行結果:
D:\Python\Python36-32\python.exe E:/Python/DAY-14/day14_練習.py
[abc, 1231, 213]

Process finished with exit code 0

Python基礎day-13[模塊:re未完]