pythonの正規表現の使い方re.findall()
59090 ワード
参考ブログアドレス:https://blog.csdn.net/YZXnuaa/article/details/79346963
re.findall ( string pattern , )
:
1 findall(pattern, string, flags
=
0
)
import re
Python re findallメソッドは、 するサブ をリスト で すことができる
print (help(re.findall))
print (dir(re.findall))
findallは、すべてのr を し、 に の を す
1
2
3 regular_v1
=
re.findall(r
"docs"
,
“https://docs.python.org/3/whatsnew/3.6.html”
)
print
(regular_v1)
# [‘docs’]
^ https ,
1
2
3 regular_v2
=
re.findall(r
"^https"
,
“https://docs.python.org/3/whatsnew/3.6.html”
)
print
(regular_v2)
# [‘https’]
h
t
m
l
,
<
/
p
r
e
>
<
d
i
v
c
l
a
s
s
=
"
c
n
b
l
o
g
s
H
i
g
h
l
i
g
h
t
e
r
s
h
−
g
u
t
t
e
r
"
>
<
d
i
v
>
<
d
i
v
c
l
a
s
s
=
"
s
y
n
t
a
x
h
i
g
h
l
i
g
h
t
e
r
p
y
t
h
o
n
"
>
<
d
i
v
c
l
a
s
s
=
"
t
a
b
l
e
−
b
o
x
"
>
<
t
a
b
l
e
c
e
l
l
s
p
a
c
i
n
g
=
"
0
"
c
e
l
l
p
a
d
d
i
n
g
=
"
0
"
b
o
r
d
e
r
=
"
0
"
>
<
t
b
o
d
y
>
<
t
r
>
<
t
d
c
l
a
s
s
=
"
g
u
t
t
e
r
"
>
<
d
i
v
c
l
a
s
s
=
"
l
i
n
e
n
u
m
b
e
r
1
i
n
d
e
x
0
a
l
t
2
"
>
1
<
/
d
i
v
>
<
d
i
v
c
l
a
s
s
=
"
l
i
n
e
n
u
m
b
e
r
2
i
n
d
e
x
1
a
l
t
1
"
>
2
<
/
d
i
v
>
<
d
i
v
c
l
a
s
s
=
"
l
i
n
e
n
u
m
b
e
r
3
i
n
d
e
x
2
a
l
t
2
"
>
3
<
/
d
i
v
>
<
/
t
d
>
<
t
d
c
l
a
s
s
=
"
c
o
d
e
"
>
<
d
i
v
>
<
d
i
v
c
l
a
s
s
=
"
l
i
n
e
n
u
m
b
e
r
1
i
n
d
e
x
0
a
l
t
2
"
>
<
c
o
d
e
c
l
a
s
s
=
"
p
y
t
h
o
n
p
l
a
i
n
"
>
r
e
g
u
l
a
r
v
3
<
/
c
o
d
e
>
<
c
o
d
e
c
l
a
s
s
=
"
p
y
t
h
o
n
k
e
y
w
o
r
d
"
>
=
<
/
c
o
d
e
>
<
c
o
d
e
c
l
a
s
s
=
"
p
y
t
h
o
n
p
l
a
i
n
"
>
r
e
.
f
i
n
d
a
l
l
(
r
<
/
c
o
d
e
>
<
c
o
d
e
c
l
a
s
s
=
"
p
y
t
h
o
n
s
t
r
i
n
g
"
>
"
h
t
m
l
html , </pre><div class="cnblogs_Highlighter sh-gutter"><div><div class="syntaxhighlighter python"><div class="table-box"><table cellspacing="0" cellpadding="0" border="0"><tbody><tr><td class="gutter"><div class="line number1 index0 alt2">1</div><div class="line number2 index1 alt1">2</div><div class="line number3 index2 alt2">3</div></td><td class="code"><div><div class="line number1 index0 alt2"><code class="python plain">regular_v3 </code><code class="python keyword">=</code> <code class="python plain">re.findall(r</code><code class="python string">"html
html , </pre><divclass="cnblogsHighlightersh−gutter"><div><divclass="syntaxhighlighterpython"><divclass="table−box"><tablecellspacing="0"cellpadding="0"border="0"><tbody><tr><tdclass="gutter"><divclass="linenumber1index0alt2">1</div><divclass="linenumber2index1alt1">2</div><divclass="linenumber3index2alt2">3</div></td><tdclass="code"><div><divclass="linenumber1index0alt2"><codeclass="pythonplain">regularv3</code><codeclass="pythonkeyword">=</code><codeclass="pythonplain">re.findall(r</code><codeclass="pythonstring">"html",
“https://docs.python.org/3/whatsnew/3.6.html”
)
print
(regular_v3)
# [‘html’]
# […]
1
2
3 regular_v4
=
re.findall(r
"[t,w]h"
,
“https://docs.python.org/3/whatsnew/3.6.html”
)
print
(regular_v4)
# [‘th’, ‘wh’]
“d” 0 9
1
2
3
4
5
6 regular_v5
=
re.findall(r
"\d"
,
“https://docs.python.org/3/whatsnew/3.6.html”
)
regular_v6
=
re.findall(r
"\d\d\d"
,
“https://docs.python.org/3/whatsnew/3.6.html/1234”
)
print
(regular_v5)
# [‘3’, ‘3’, ‘6’]
print
(regular_v6)
# [‘123’]
d 0-9, D ,
1
2
3 regular_v7
=
re.findall(r
"\D"
,
“https://docs.python.org/3/whatsnew/3.6.html”
)
print
(regular_v7)
# [‘h’, ‘t’, ‘t’, ‘p’, ‘s’, ‘:’, ‘/’, ‘/’, ‘d’, ‘o’, ‘c’, ‘s’, ‘.’, ‘p’, ‘y’, ‘t’, ‘h’, ‘o’, ‘n’, ‘.’, ‘o’, ‘r’, ‘g’, ‘/’, ‘/’, ‘w’, ‘h’, ‘a’, ‘t’, ‘s’, ‘n’, ‘e’, ‘w’, ‘/’, ‘.’, ‘.’, ‘h’, ‘t’, ‘m’, ‘l’]
“w” a z, A Z, 0 9
1
2
3 regular_v8
=
re.findall(r
"\w"
,
“https://docs.python.org/3/whatsnew/3.6.html”
)
print
(regular_v8)
#[‘h’, ‘t’, ‘t’, ‘p’, ‘s’, ‘d’, ‘o’, ‘c’, ‘s’, ‘p’, ‘y’, ‘t’, ‘h’, ‘o’, ‘n’, ‘o’, ‘r’, ‘g’, ‘3’, ‘w’, ‘h’, ‘a’, ‘t’, ‘s’, ‘n’, ‘e’, ‘w’, ‘3’, ‘6’, ‘h’, ‘t’, ‘m’, ‘l’]
“W”
1
2
3 regular_v9
=
re.findall(r
"\W"
,
“https://docs.python.org/3/whatsnew/3.6.html”
)
print
(regular_v9)
# [’:’, ‘/’, ‘/’, ‘.’, ‘.’, ‘/’, ‘/’, ‘/’, ‘.’, ‘.’]