程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Regular expression (Python)

編輯:Python

Preface

The application of regular expressions is mainly investigated from three aspects ( Basic metacharacters / Common regular expressions / use python re Module resolution )

Table of contents title

  • Preface
  • One . Basic metacharacters
  • Two . Common regular expressions
  • 3、 ... and
  • summary

One . Basic metacharacters

  • Ask what special characters of regular expressions mean

\b
\B
\w
\W
\d
\D
\s
\S
.
*
+
?
|

Please search the above characters again , See what it means

  • Greedy mode and non greedy mode
  • What characters are needed \ To escape
  • Chinese coding range

Two . Common regular expressions

This part of the expression is not unique , Because the needs are different .

  • cell-phone number
^1[3-9]\d{
9}$
^1[^0-2]\d{
9}$

Blur the mobile phone number , Blur the middle four digits

import re
phone = '18728147811'
# \1 and \3 It refers to obtaining the matching value of the first group and the third group 
res = re.sub(r"(\d{3})(\d{4})(\d{4})", r"\1****\3", phone)
print(res)
#187****7811
  • mailbox
" for example [email protected]"
^[A-Za-z0-9_-][email protected][A-Za-z0-9_-]+(\.[A-Za-z0-9_-]+)+$
  • user name
" Alphanumeric underscores support Chinese 4 To 12 position "
^[A-Za-z0-9\u4e00-\u9fa5_]{
4,12}$
  • password
 Here are the password registration requirements for CET-4 and CET-6 website
"8-15 Bit length ( Case sensitive ); Password contains both : Capital 、 Lowercase letters 、 Numbers and special characters "
" Special characters are [email protected]#$%^&*-_"
^(?=.*\d+)(?=.*[a-z])(?=.*[A-Z])(?=.*[!@#%_-&\$\^\*])^.{8,15}$

3、 ... and

summary


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved