regular expression for Python
regular expression for Python
Python and regular expression
Python 공식 문서 참고 언어마다 사용법이 다르다고 한다.
example code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# regular expression test
import re # regular standard lib
def testRegexp():
pattern = "c[a-d]v?x*" # = {ca,cb,cc,cd,cax,caxx}
regex = re.compile(pattern)
pattern2 = "c[a-d]v?x*cxcx*"
regex2 = re.compile(pattern2)
# search
res = regex.search("cbvxxxxxxxxxxxxcxcxx")
print(res)
res2 = regex2.search("cbvxxxxxxxxxxxxcxcxx")
print(res2)
return None
def testSSID():
pattern = "[0-9]{6}-[0-9]{7}"
regex = re.compile(pattern)
res = regex.search("999999-8888808")
print(res)
return None
def main():
testRegexp()
testSSID()
return None
if __name__ == '__main__':
main()
1
2
3
<re.Match object; span=(0, 15), match='cbvxxxxxxxxxxxx'>
<re.Match object; span=(0, 20), match='cbvxxxxxxxxxxxxcxcxx'>
<re.Match object; span=(0, 14), match='999999-8888808'>
This post is licensed under CC BY 4.0 by the author.