Python: Regular Expression module, python: Module

Source: Internet
Author: User

Python: Regular Expression module, python: Module

1. Regular Expression

Regular expressions are used to match strings. Regular expressions have their own rules, which are irrelevant to python and match strings.

2, character group

The various characters that may appear at the same position constitute a character group, which is represented by [] in a regular expression. There are many types of characters, such as numbers, letters, and punctuation. If you want to "only show one number" at a position, the character at this position can only be one of the 10 numbers 0, 1, 2... 9.
3. Check whether the entered mobile phone number is valid.
# Check whether the entered mobile phone number is valid while True: phone_number = input ('Please input your phone number: ') if len (phone_number) = 11 \ and phone_number.isdigit () \ and (phone_number.startswith ('13') \ or phone_number.startswith ('14') \ or phone_number.startswith ('15') \ or phone_number.startswith ('18 ')): print ('legitimate mobile phone number') else: print ('invalid mobile phone number ')

4. Use another method: Call the re module to check whether the mobile phone number is valid.

Import rephone_number = input ('Please input your phone number: ') if re. match ('^ (13 | 14 | 15 | 18) [0-9] {9} $', phone_number): print ('legitimate phone number') else: print ('invalid mobile phone number ')

5. Regular Expression rules

Character:

Quantifiers

6. escape characters have special meanings in python and regular expressions, such as \ n, which indicates line breaks in python, if you want to output \ n directly, there is no way to achieve it. Below are some methods for outputting escape characters.

Escape Character
Regular Expression With matching characters Matching result Description
\ N \ N False
Because \ is a special character in a regular expression, to match \ d itself, the expression \ d cannot match
\ N \ N True
Escape \ and change to \ to match
\\\\ N \ N True
In python, '\' in the string also needs to be escaped, so each character string '\' needs to be escaped again
R' \ d'
R' \ d'
True
Add r before the string so that the entire string is not escaped.

Regular Expression test: http://tool.chinaz.com/regex/

7. Greedy match

1) What is greedy matching?

When matching is met, match the string as long as possible. By default, greedy match is used. For example: *. If you repeat zero or more times, the default value is greedy match. Therefore, the default value is more than zero.

Regular Expression Characters to be matched Matching result Description
<. *> <Script>... <script> <Script>... <script>
The default value is the greedy match mode, which matches strings as long as possible.
<. *?> R' \ d' <Script>
<Script>
Plus? To convert the greedy match mode to the non-Greedy match mode, strings that are as short as possible are matched.

2 )? In the regular expression usage summary:

Quantifiers: match once or once after quantifiers: Put the inertia operation in the group: cancel the Group first 3) several common non-Greedy match Pattern
*? Repeat any time, but as few as possible +? Repeat once or more times, but as few as possible ?? Repeated 0 or 1 times, but as few as possible {n, m }? Repeat n to m times, but try to repeat {n ,}? Repeated more than n times, but as few as possible

8. Common Methods of re Module

Findall returns all matching results and puts them in the list. findall ('content for predicate search ', 'here content to be searched.') search returns one from the past to the next, and an object (variable) that returns a result. The returned variable must be called. group () to obtain the result ., If no match exists, None is returned. If you call group (), the following error is returned: search ('content to be searched first ', 'content to be searched here'). match and search are used exactly the same, match matches from the beginning. If a regular rule is found from the beginning, a variable is returned. You must use a group to display the matched content. If no match is found, None is returned. If you call match, an error is returned. Match ('content to be queried ', 'content to be searched') split Based on the regular relationship. Split ('regular rule', 'replaced content ')
Ret = re. split ("\ d +", "eva3egon4yuan") print (ret) # result: ['eva ', 'egon', 'yua']

After grouping:

Ret = re. split ("(\ d +)", "eva3egon4yuan") print (ret) # result: ['eva ', '3', 'egon', '4 ', 'yuany']
Sub: Replace sub ('regular rule', 'replaced content', 'replaced content', replacement quantity) subn ('regular rule', 'replaced content ', 'replaced content') # The returned result is ('regular rule', 'replaced content', 'replaced content', replacement times) finditer ('regular rule ', 'searched content') # returns an iterator that stores matching results. For finditer and split, if their regular expressions contain groups, the group content takes precedence over 9 and flags.

 

Flags has many optional values: re. I (IGNORECASE) is case-insensitive and contains a complete record in parentheses. M (MULTILINE) MULTILINE mode, changing the ^ and $ behavior re. S (DOTALL) point can match any character, including the line break re. L (LOCALE) for local recognition matching, indicating that the special character set \ w, \ W, \ B, \ B, \ s, \ S depends on the current environment, re is not recommended. the usage of U (UNICODE) \ w \ W \ s \ S \ d \ D depends on the Character attribute defined by unicode. In python3, The flagre. X (VERBOSE) Lengthy mode is used by default. In this mode, the pattern string can be multiline, ignore blank characters, and add comments to flags.

 

Related Article

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.