天天看點

正則的簡單内容

import re
#正則常用的類型
#1單個字元
#2‘.’字元後面任意一個字元
#3.\ 表示轉義
#4.^ 表示從這裡開始
#5.$  表示以這裡結尾
#6 |d   表示比對一個數字字元集   0-9之間


# line="huang1234"
# #比對
# match_res=re.match('huang1234',line)
# #需要比對的字元串。。。看能不能比對成功
#
# if match_res:
#     print('比對成功')
# else:
#     print('比對失敗')

# line='hs423d'
# match_res=re.match('h.',line)
# if match_res:
#     print('比對成功')
# else:
#     print('比對失敗')
#
# line='hs423d'
# match_res=re.match('h.$',line)
# if match_res:
#     print('比對成功')
# else:
#     print('比對失敗')

# line='whh3'
# match_res=re.match('.*3$',line)
# if match_res:
#     print('比對成功')
#     print(match_res)
# else:
#     print('比對失敗')

line='whh3'
match_res=re.match('h\.*3$',line)
if match_res:
    print('比對成功')
    print(match_res)
else:
    print('比對失敗')      

繼續閱讀