Skip to content

Commit 2c700e8

Browse files
committed
Finished chapter 7
1 parent 8606afd commit 2c700e8

File tree

6 files changed

+126
-0
lines changed

6 files changed

+126
-0
lines changed

.vscode/settings.json

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"python.linting.pylintEnabled": false
3+
}

07-regex/isPhoneNumber.py

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
def isPhoneNumber(text):
2+
if len(text) != 12:
3+
return False # not phone number-sized
4+
for i in range(0, 3):
5+
if not text[i].isdecimal():
6+
return False # not an area code
7+
if text[3] != '-':
8+
return False # does not have first hyphen
9+
for i in range(4, 7):
10+
if not text[i].isdecimal():
11+
return False # does not have first 3 digits
12+
if text[7] != '-':
13+
return False # does not have second hyphen
14+
for i in range(8, 12):
15+
if not text[i].isdecimal():
16+
return False # does not have last 4 digits
17+
return True # "text" is a phone number!
18+
19+
print('415-555-4242 is a phone number:')
20+
print(isPhoneNumber('415-555-4242'))
21+
print('Moshi moshi is a phone number:')
22+
print(isPhoneNumber('Moshi moshi'))

07-regex/passwordValidation.py

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import re
2+
3+
def passwordValidation(pwd):
4+
if(len(pwd) < 8):
5+
return False
6+
upperRegex = re.compile(r'[A-Z]')
7+
lowerRegex = re.compile(r'[a-z]')
8+
numberRegex = re.compile(r'[0-9]')
9+
10+
if upperRegex.search(pwd) and lowerRegex.search(pwd) and numberRegex.search(pwd):
11+
return True
12+
else:
13+
return False
14+
15+
def printResult(pwd):
16+
print(pwd.ljust(20), passwordValidation(pwd))
17+
18+
19+
printResult('HelloWorld123')
20+
printResult('HelloWorld')
21+
printResult('Hello123')
22+
printResult('H')
23+
printResult('elloorld123')
24+
printResult('HW123')

07-regex/phoneAndEmail.py

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#! python3
2+
# phoneAndEmail.py - Finds phone numbers and email addresses on the clipboard.
3+
4+
import pyperclip, re
5+
6+
phoneRegex = re.compile(r'''(
7+
(\d{3}|\(\d{3}\))? # area code
8+
(\s|-|\.)? # separator
9+
(\d{3}) # first 3 digits
10+
(\s|-|\.) # separator
11+
(\d{4}) # last 4 digits
12+
(\s*(ext|x|ext.)\s*(\d{2,5}))? # extension
13+
)''', re.VERBOSE)
14+
15+
# Create email regex.
16+
emailRegex = re.compile(r'''(
17+
[a-zA-Z0-9._%+-]+ # username
18+
@ # @ symbol
19+
[a-zA-Z0-9.-]+ # domain name
20+
(\.[a-zA-Z]{2,4}){1,2} # dot-something
21+
)''', re.VERBOSE)
22+
23+
# Find matches in clipboard text.
24+
text = str(pyperclip.paste())
25+
26+
matches = []
27+
for groups in phoneRegex.findall(text):
28+
phoneNum = '-'.join([groups[1], groups[3], groups[5]])
29+
if groups[8] != '':
30+
phoneNum += ' x' + groups[8]
31+
matches.append(phoneNum)
32+
for groups in emailRegex.findall(text):
33+
matches.append(groups[0])
34+
35+
# Copy results to the clipboard.
36+
if len(matches) > 0:
37+
pyperclip.copy('\n'.join(matches))
38+
print('Copied to clipboard:')
39+
print('\n'.join(matches))
40+
else:
41+
print('No phone numbers or email addresses found.')

07-regex/readme.md

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
虽然在 Python 中使用正则表达式有几个步骤,但每一步都相当简单。
2+
1. 用 import re 导入正则表达式模块。
3+
import re
4+
2.用 re.compile()函数创建一个 Regex 对象(记得使用原始字符串)。
5+
regexObj = re.compile(r'\d{3}-\d{3}-\d{4}')
6+
3.向 Regex 对象的 search()方法传入想查找的字符串。它返回一个 Match 对象。
7+
mo = regexObj.search(strObj)
8+
4.调用 Match 对象的 group()方法,返回实际匹配文本的字符串。
9+
mo.group()
10+
11+
regex test site:
12+
http://www.regexpal.com/
13+
14+
花括号的“非贪心” 版本匹配尽可能最短的字符串,即在结束的花括号后跟着一个问号。
15+
16+
\d 0 到 9 的任何数字
17+
\D 除 0 到 9 的数字以外的任何字符
18+
\w 任何字母、数字或下划线字符(可以认为是匹配“单词”字符)
19+
\W 除字母、数字和下划线以外的任何字符
20+
\s 空格、制表符或换行符(可以认为是匹配“空白”字符)
21+
\S 除空格、制表符和换行符以外的任何字符
22+
23+
24+
通过传入 re.DOTALL 作为 re.compile()的第二个参数, 可以让句点字符匹配所有字符, 包括换行字符。

07-regex/stripRe.py

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import re
2+
3+
def stripRe(s):
4+
#stripRe = re.compile(r'^\s+\(.*\)\s+$')
5+
stripRegex = re.compile(r'^\s+(.*)\s+$')
6+
mo = stripRegex.search(s)
7+
return mo.group(1)
8+
9+
10+
strTest = ' hello world '
11+
print(strTest)
12+
print(stripRe(strTest))

0 commit comments

Comments
 (0)