Python regular modules and regular expressions

Source: Internet
Author: User

What is a module?

Common scenario: A module is a file that contains Python definitions and declarations, and the file name is the suffix of the module name plus the. Py.

In fact, the import loaded module is divided into four general categories:

1 code written using Python (. py file)

2 C or C + + extensions that have been compiled as shared libraries or DLLs

3 packages for a set of modules

4 built-in modules written and linked to the Python interpreter using C

Why use a module?

If you quit the Python interpreter and then re-enter, then the functions or variables you defined previously will be lost, so we usually write the program to a file so that it can be persisted and executed in Python test.py when needed, when test.py is called a scripting script.

With the development of the program, more and more functions, in order to facilitate management, we usually divide the program into a file, so that the structure of the program is clearer and easier to manage. At this point, we can not only use these files as scripts to execute, but also as a module to import into other modules, to achieve the function of reuse

How do I use modules?

A module can contain definitions of executable statements and functions that are intended to initialize modules that execute only when the module name is first encountered when importing an import statement (the import statement can be used anywhere in the program and is imported multiple times for the same module. To prevent you from repeating the import, Python is optimized by loading the module name into memory after the first import, followed by an import statement that only adds a reference to the module object that has loaded the large memory, and does not re-execute the statements within the module.

Each module is a separate namespace, defined in this module of the function, the namespace of the module as the global namespace, so that when we write our own module, we do not have to worry about our definition in our own module global variables will be imported, and the user's global variables conflict

Regular expressions

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.
Online test Tool http://tool.chinaz.com/regex/

Character group: The range of characters that can appear in the same position.

    The various characters that may appear in the same location make up a group of characters,

In regular expressions, [] represents characters in many 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.

Regular
Characters to match
The
Results
Description
[0123456789]
8
True
Enumerates all the valid characters in a group of characters, any character in a character group
The same as the "to match" character is considered to match
[0123456789]
A
False
Cannot match because there is no "a" character in the character group
[0-9]
7
True
can also be used-to denote a range, [0-9] and [0123456789] is a meaning
[A-z]
S
True
Similarly, if you want to match all lowercase letters, you can simply use [A-z] to represent
[A-z]
B
True
[A-z] means all uppercase letters
[0-9a-fa-f]
E
True
Can match a number, case-a~f, to validate hexadecimal characters

Character:

Metacharacters
Match content
. Match any character other than line break
\w Match letters or numbers or underscores
\s Match any of the whitespace characters
\d Match numbers
\ n Match a line break
\ t Match a tab
\b Match the end of a word
^ Match the start of a string
$ Match the end of a string
\w
Match a non-letter or number or underscore
\d
Match non-numeric
\s
Match non-whitespace characters
A|b
Match character A or character B
()
Matches an expression within parentheses, and also represents a group
[...]
Match characters in a character group
[^...]
Match all characters except characters in a character group

Quantifiers:

Quantifiers
Usage Notes
* Repeat 0 or more times
+ Repeat one or more times
? Repeat 0 or one time
N Repeat n times
{N,} Repeat N or more times
{N,m} Repeat N to M times

Python regular modules and regular expressions

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.