Python 3の正則re.X修飾子

889 ワード


X(VERBOSE)
このオプションは、ルール式の空白とコメントを無視し、'#'を使用してコメントを起動できます.これでルールをもっと美しく書くことができます.
 
コードの例:
#        

import re

rc = re.compile(r"""
# start a rule

/d+
# number

| 

[a-zA-Z]+
# word

""", re.X)
res = rc.match('aaaa')
# print(res)
print(res.group())


    :
F:\win10software\Python36\python36.exe
aaaa

Process finished with exit code 0



#     

import re

rc = re.compile(r"""
#       
#          ,    "\s+"  
\  +

""", re.X)
res = rc.match('   11')  #          
print(res)
print('='*30)
print('=%s=' % res.group())

#     
F:\win10software\Python36\python36.exe
<_sre.sre_match object="" span="(0," match="   ">
==============================
=   =

Process finished with exit code 0