"""
Syntax 1: get all exceptions
try:
Code snippet 1
except:
Code snippet 2
explain:
try, except - keyword, fixed method
: - Fixed writing
Code snippet 1 - one or more statements that keep an indent with try; code that may have exceptions (code that needs to catch exceptions)
Snippet 2 - exception caught, code to execute
Implementation process:
Code segment 1 is executed first. If there is an exception in the execution process, code segment 2 will be executed directly; if there is no exception in code segment 1, code segment 2 will not be executed
"""
#Exercise: enter the age. If you enter it incorrectly, you will be prompted to enter the wrong information
# try:
#age = int(input('Please enter age: ')
#print('input completed! )
# except:
#print('wrong age input! )
#
# print('===================')
try:
#age = int(input('Please enter age: ')
print({'name':'zhang San '} ['age'])
print([1, 2, 3][10])
except:
print('exception occurred ')
"""
Syntax 2: capture the exception of the specified type
try:
Code snippet 1
Exception except ion type:
Code snippet 2
Execution process: execute code segment 1 first. If there is an exception in code segment 1, judge whether the type of exception occurred is consistent with the exception type after exception. If consistent program does not crash, code segment 2 will be executed directly, and inconsistent program will crash directly.
"""
# [10, 20][100] # IndexError
#{name ':'zhang San'} ['age '] # keyerror
try:
print({'name':'zhang San '} ['age'])
print([10, 20][100])
except KeyError:
print('exception! )
"""
Syntax 3: capture multiple exceptions at the same time, and do the same processing for different exceptions
try:
Code snippet 1
Exception (exception type 1, exception type 2,...)
Code snippet 2
Syntax 4: capture multiple exceptions at the same time, and do different processing for different exceptions
try:
Code snippet 1
Exception except ion type 1:
Code snippet 11
Exception except ion type 2:
Code snippet 22
Exception except ion type 3:
Code snippet 33
...
"""
"""
fullmatch(regular expression , character string) - Let the regular expression match the string exactly. If the match fails, the result is None
js Regularity of: /regular expression /
python Regularity of: r'regular expression '
"""
print('=====================1.Matching symbol=====================')
re_str = r'abc'
result = fullmatch(re_str, 'abc')
print(result)
re_str = r'.abc'
result = fullmatch(re_str, '+abc')
print(result)
re_str = r'.abc.'
result = fullmatch(re_str, '(abc+')
print(result)
re_str = r'...'
result = fullmatch(re_str, 'ajs')
print(result)
re_str = r'\wabc'
result = fullmatch(re_str, '8abc')
print(result)
re_str = r'\d\d\d'
result = fullmatch(re_str, '142')
print(result)
re_str = r'\d\dabc\d\d'
result = fullmatch(re_str, '23abc89')
print(result)
re_str = r'\s\d..'
result = fullmatch(re_str, '\n9k/')
print(result)
re_str = r'\d\d\s\d\d'
result = fullmatch(re_str, '78 23')
print(result)
"""
\letter - The function of lower case letters is opposite to that of corresponding capital letters
"""
re_str = r'\dabc\D'
result = fullmatch(re_str, '8abc-')
print(result)
re_str = r'\Sabc'
result = fullmatch(re_str, '=abc')
print(result)
"""
//Note: a [] can only match one character
a.
[abc123] - matching a,b,c,1,2,3 Any character in
b.
[a-z] - Match from character a To character z Any character between(Match any lowercase letter)
[A-Z] - Match any capital letter
[a-zA-Z] - Match any letter
[0-9] - Match any numeric character
[\u4e00-\u9fa5] - Match any Chinese character
"""
re_str = r'[cz+?]123'
result = fullmatch(re_str, '?123')
print(result)
re_str = r'[\u4e00-\u9fa5]123'
result = fullmatch(re_str, 'Look at 123')
print(result)
re_str = r'1[3-9]\d\d\d\d\d\d\d\d\d'
result = fullmatch(re_str, '13598902763')
print(result)
result = fullmatch(r'[-09]abc', '-abc')
print(result)
re_str = r'[a-zA-Z\d_]abc'
result = fullmatch(re_str, '_abc')
print(result)
"""
[^\u4e00-\u9fa5] - Match any non Chinese character
[^0-9] - Matches any non numeric character
[^a-zA-Z] - Matches any non alphabetic character
"""
print(fullmatch(r'[abc^]123', 'b123'))
print(fullmatch(r'[^abc]123', 'a123'))
print('=====================2.Detection symbol======================')
"""
//Word boundary: the beginning of a string, the end of a string, any symbol that can distinguish two different words
//Note: the symbol of the detection class does not affect the length of the match, but only makes further detection when the matching is successful
"""
message = 'how are you?i am fine!thank you!'
re_str = r'\d\d.\b\d\d'
print(fullmatch(re_str, '56=89'))
re_str = r'\d^abc'
print(fullmatch(re_str, '1abc'))
re_str = r'^\d\d\d'
print(fullmatch(re_str, '678'))
re_str = r'^\d\d\d'
print(fullmatch(re_str, '678'))
print(search(re_str, 'shdj39 Geek time 238 u 282='))
re_str = r'\d\d$'
print(search(re_str, 'Times peak 78 hint method 23 sofa 89'))
print('=======================3.Matching times==================')
"""
//Character * - character 0 or more times
"""
re_str = r'a*'
print(fullmatch(re_str, 'aaa'))
re_str = r'\d*'
print(fullmatch(re_str, '478923'))
re_str = r'123[a-z]*'
print(fullmatch(re_str, '123ukl'))
re_str = r'a+'
print(fullmatch(re_str, 'a'))
re_str = r'\d?abc'
print(fullmatch(re_str, '0abc'))
re_str = r'[-+]?\d+'
print(fullmatch(re_str, '+23874'))
"""
{N} - matching N second
{M,N} - matching M reach N second
{M,} - Match at least M second
{,N} - Most matches N Times (0~N Times)
"""
re_str = r'\d{4}abc'
print(fullmatch(re_str, '6723abc'))
re_str = r'a{2,5}123'
print(fullmatch(re_str, 'aaaaa123'))
re_str = r'a{2,}123'
print(fullmatch(re_str, 'aaaaaaaaaaa123'))
re_str = r'a{,2}123'
print(fullmatch(re_str, 'aa123'))
"""
//In the case of uncertain matching times, there are two matching patterns: greedy and non greedy
a.Greedy: greedy by default (match as many times as possible on the premise of matching)
*,+,?, {M,N},{M,},{,N}
b.Non Greed:(On the premise of matching, the matching times should be as few as possible)When the matching times are uncertain, a question mark is added after the number of times, and the matching is non greedy
*?,+?,??,{M,N}?,{M,}?,{,N}?
"""
re_str = r'\d{2,}'
print(search(re_str, 'Nurse 227382 abc Hello!'))
re_str = r'\d{2,}?'
print(search(re_str, 'Nurse 227382 abc Hello!'))
content = read_file('data.json')
re_str = r'"provinceName":"(.+?)",'
print(findall(re_str, content))