Python full stack learning --day31 (regular)




try: '''Code where exceptions may occur''' except ValueError: ''''Print some tips or processing content''' except NameError: '''...''' except Exception: '''Universal exceptions cannot be used indiscriminately'''

 

try:
    '''Code where exceptions may occur'''
except ValueError:
    ''''Print some tips or processing content'''
except NameError:
    '''...'''
except Exception:
    '''Universal exceptions cannot be used indiscriminately'''
else:
    '''All the above except are not executed'''

  

try:
    '''Code where exceptions may occur'''
except ValueError:
    '''Print some tips or processing content'''
else:
    '''The code in the try is executed normally'''
finally:
    '''Whether an error occurs or not, this code will be executed to do some beginning and end work'''

  

number = input('please input your phone number:')
if number.isdigit() and number.startswith('13')\
    or number.startswith('14')\
    or number.startswith('15')\
    or number.startswith('16')\
    or number.startswith('17')\
    or number.startswith('18')\
    or number.startswith('19'):
    print('Pass check')
else:
    print('Format error')

The above code is too verbose, use regular
number = input('please input your phone number:')
ret = re.match('(13|14|15|16|17|18|19)[0-9]{9}',number)
if ret:print('passed the initial check')

Example one:

Match the mobile phone number, you can use the regular.

with open('a',encoding='utf-8')as f1:
    li = []
    for i in f1:
        i = i.strip()
        ret = re.findall('1[3-9]\d{9}',i)
        li.extend(ret) #extend merge
print (li)
The regular expression itself has nothing to do with python, it is a rule for matching the content of a string.
Official definition: A regular expression is a logical formula for operating on strings, which is to use some pre-defined specific characters and combinations of these specific characters to form a "rule string". This "rule string" is used to Express a kind of filtering logic on strings.

  regular expression

Online testing tool   http://tool.chinaz.com/regex/

This is the best regex tool
regex can be matched at any time

shortcoming:
If you only use this tool and don't write it yourself. Not anymore. Mainly write it yourself, don't rely too much on it.
Character group String is represented by []
it can only match a string

 

 

 

Then we need to consider more about the range of characters that can appear in the same position. 
Character group: [Character group]
Various characters that may appear in the same position form a character group. In regular expressions, use [] to indicate that the
characters are divided into many categories, such as numbers, letters, punctuation and so on.
If you now require a position "only one number can appear", then the character at this position can only be one of 10 numbers 0, 1, 2...9.
[9-0] No, why?
it compares ascii codes
 
[5-9] This is possible
[5.5-9] This is not allowed
Decimal point is not allowed
 
match 3 digits

match uppercase

 

 case match

[Az] It's wrong to write that. The case of ascii code is not consecutive
it cannot match special characters
 
 
 [0-9a-fA-F] means match hex
 Summarize:

. is omnipotent, except for newlines

match whitespace

focus

^
matches the beginning of the string
$
matches the end of the string

 

starts with the sea

Regular expression, cannot be written after

It can only appear at the beginning, not in the middle or at the back

In this case, it is the only ke that can be placed anywhere.

Indicates that it matches more than 11 digits and cannot be less than 11 digits.

Match 15 digits, more matches

* means 0 or more times.
This matches 0 times, although it doesn't match.
 

Match 1 or more times

? Match once or 0 times
It doesn't match, it's 0 times

 

 Highlights:

Quantifiers can only constrain one string

Constraints here [A-Z]

Constrain [0-9] and [AZ] simultaneously

If there is no match, it is matched 0 times
match 0 times

tool.chinaz.com/regex/
The results displayed by this tool may be different from the real ones
? means 0 or more times
The default regex is greedy matching
If it can match 1 time, it will never match 0 times
.* means match all
 
 

Non-greedy match, plus one? , is non-greedy
Within the rules, less is better

 

up to 2 times

 With red line, not important

 

Meta characters, should be used with quantifiers

group() AND OR | [^]
The ID card number is a string with a length of 15 or 18 characters. If it is 15 digits, it is all composed of numbers, and the first digit cannot be 0; if it is 18 digits, the first 17 digits are all digits, and the last digit may be a number or x, let's try to use regular expressions below:

 

 

Requirements for Two Constraints

bad writing

 

 

Match at least 15 times

It's more professional

Escapes \
Now to match \n, the slashes need to be escaped

2 underscores are escaping

 

 

greedy match
Greedy matching: When matching is satisfied, match the longest possible string. By default, greedy matching is used

the result is a

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325022937&siteId=291194637