Python Full stack learning--day31 (Regular)

Source: Internet
Author: User




Try: ' The code of the exception may appear ' except ValueError: ' print some hints or handle the content ' ' except Nameerror: ' ' ... ' except Exception: "' universal anomaly cannot be used indiscriminately '

 

Try:    ' The code of the exception may appear ' except ValueError: '    print some hints or handle the content ' ' except Nameerror: ' '    ... ' except Exception:    ' ' Universal exception cannot be used ' ' ' else: '    all except are not executed '

  

Try:    "The code for the exception may appear" ' except ValueError: '    print some hints or handle the content ' ' else: ' The    code in ' try ' normally executes ' finally:    "" regardless of whether or not the error occurred, will execute this code, to do some end-to-end work "

  

Number = input (' Please input your phone number: ') if Number.isdigit () and Number.startswith (' + ')    or Number.startswith (' + ') or number.startswith (' + ') or    number.startswith (' + ')    or Number.startswith (' + ')    or number.startswith (' + ')    or number.startswith (' + '):    print (' by 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 (' through initial check ')

Example one:

Match the 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 does not have anything to do with Python, it is a rule that matches the contents of a string. Official definition: A regular expression is a logical formula for string manipulation, which is a "rule string" that is used to express a filter logic for a string, using predefined specific characters and combinations of these specific characters.

Regular expressions

Online test Tool http://tool.chinaz.com/regex/

This is the best regular expression tool that can be matched at any time.

Cons: If you only use this tool and don't write it yourself. Will not. The main is to write their own, do not rely on it too. A character group string with [] means that it can only match one string

Then we'll consider more of the range of characters that can appear in the same position.
Character groups: [character groups]
The various characters that may appear in the same location make up a group of characters, expressed in a regular expression using []
Characters are divided into classes, such as numbers, letters, punctuation, and so on.
If you now ask for a position "only one number can appear", then the character in this position can only be one of the 10 numbers, 0, 1, 2...9.
[9-0] It is not possible, why? It compares the ASCII code [5-9] This is possible [5.5-9] This is not allowed to not allow the output of the decimal point to match 3 digits

Match uppercase

Match case

It's not right to write [a-z]. The case of the ASCII code is not contiguous and it cannot match the special character [0-9a-fa-f] to indicate a matching hexadecimal summary:

. is omnipotent, except for line breaks

Match Blank

Focus

^ Match the start of a string
$ Match the end of a string

Start with the sea

Regular expression, cannot be written in the back

It can only be in the starting position, not in the middle or in the back position.

In this case, the only ke can be placed in any position

Indicates a match of more than 11 bits, not less than 11 bits

Match 15-bit, toward multiple matches

* indicates 0 or more times. This matches 0 times, although there is no match.

Match 1 or more times

? Matches one or 0 matches, or 0 times.

Focus:

Quantifiers can only constrain one string

constraint here [A-z]

simultaneous constraints [0-9] and [A-z]

No match, just match 0 matches 0 times

tool.chinaz.com/regex/This tool, the results shown, may be different from the real one? Indicates that 0 or more default regular is greedy match if can match 1 times, never match 0 times. * Indicates matching all

Non-greedy match, plus one? , is not greedy in the rules, the less the better

Up to 2 times

With a red line, not important.

Metacharacters, should and quantifier use

Group () and or |[^] The ID number is a 15 or 18 character string, if 15 bits are all composed of numbers, the first cannot be 0, if 18 bits, the first 17 bits are all numbers, the last digit or x, we try to use the regular to represent:

Requirements on two constraints

It's not good to write.

Match at least 15 times

This is a more professional way of writing.

Escape character \ Now to match \ n, slash needs to be escaped

2 underscores are escaped

Greedy match greedy match: matches the string as long as possible when matching matches, by default, greedy match

The result is a

Because it's going to be hard to go back, so it's as much as possible, match a little bit more than once until you meet < stop

? First match the back.

Several commonly used non-greedy matching pattern

Match character Description
*? Repeat any number of times, but repeat as little as possible
+? Repeat 1 or more times, but repeat as little as possible
?? Repeat 0 or 1 times, but repeat as little as possible
{n,m}? Repeat N to M times, but repeat as little as possible
{N,}? Repeat more than n times, but repeat as little as possible

Python Full stack learning--day31 (Regular)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.