当前位置:网站首页>Regular expressions: examples (1)

Regular expressions: examples (1)

2022-07-06 01:38:00 Live up to your youth

1、 user name

The user name can contain the following characters :
1、26 Upper and lower case English letters are expressed as a-zA-Z.
2、 The number is expressed as 0-9.
3、 The underline is indicated by _.
4、 A dash in the middle indicates -.

The user name consists of several letters 、 Numbers 、 Underline and underline form , So you need to use + Express 1 Appear several times or more . According to the above conditions, the expression of user name can be :

[a-zA-Z0-9-_]+
>>> import re
>>> pattern = r"[a-zA-Z0-9_-]+"
>>> text = "lily123"
>>> match = re.search(pattern, text)
>>> match
<re.Match object; span=(0, 7), match='lily123'>

2、HTML label

matching html Medium <p> label :

<p((\s\S)*)<\/p>
>>> import re
>>> text = '''<p class="odd" id="odd">123</p>'''
>>> pattern = r"<p(([\s\S])*)<\/p>"
>>> re.search(pattern, text)
<re.Match object; span=(0, 31), match='<p class="odd" id="odd">123</p>'>

3、 cell-phone number

The first digit of the mobile phone number is 1, The second digit cannot be 0、1、2, Altogether 11 position . The regular expression obtained from the above conditions is :

^1[^012]\d{9}$
>>> import re
>>> pattern = r"^1[^012]\d{9}$"
>>> phone_number = "13174335288"
>>> match = re.search(pattern, phone_number)
>>> match
<re.Match object; span=(0, 11), match='13174335288'>
原网站

版权声明
本文为[Live up to your youth]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207060133468586.html