Python-Modular path Regular expression

Source: Internet
Author: User

Explain the simple module

What is a collection of modules for a set of functions
you have to deal with something, and this thing itself is not related to Python, and this thing itself exists .
Python provides a collection of features that are specifically responsible for dealing with this thing .

Types of modules
built-in modules do not require our own installed interpreter to bring the
third-party modules require our own modules to be installed
Custom Modules We write our own modules

who managed all the hardware? operating system
the file is stored on the hard disk
read files from the hard disk

Import OS # module
os.remove () # Delete file
Os.rename () # Renaming files

The module is actually providing us with functionality
The content to be manipulated is already there.
The module is just a way that Python provides to us to manipulate this content.

Why should I have a module
How to use the module

The regular expression itself does not have anything to do with Python, it is a rule that matches a string.
official definition: A regular expression is a logical formula for the manipulation of a string, which is the use of a predefined set of characters, and a combination of those specific characters ,
form a "rule string" that is used to express a filtering logic for a string.

Regular Expressions:

Say the rules I already know you're dizzy, now let's look at some practical applications. Online test Tool http://tool.chinaz.com/regex/
The first thing you need to know is that when it comes to the regular, it's only related to strings. In the tool I have given you, every word you enter is a string.
Second, if a value in a position does not change, then no rules are required.
For example you want to use "1" to match "1", or "2" to Match "2", directly can match on. This can be done easily with Python's string operations.
So after that, we're going to have to think more aboutin the same positionThe range of characters that can appear
character Group: [Character Group] the various characters that may appear in the same position make up a group of characters, and in regular expressions the 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.

Character:

Quantifiers:

. ^ $ :

* + ? {} :

NOTE: The preceding *, +,? And so on are greedy matches, and match as much as possible, followed by the number to make it an inert match

Character Set [] [^]

Group () and OR | [ ^ ]

The ID number is a string of 15 or 18 characters long, and if 15 bits all have numbers, the first cannot be 0;

If it is 18 bits, then the first 17 digits are all numbers, the last may be numbers or x, below we try to use the regular to represent:

Escape character \

In regular expressions, there are many special meanings of meta-characters, such as \d and \s, if you want to match the normal "\d" instead of the number will need to escape the "\" into "\ \".

In Python, either the regular expression or the content to be matched is in the form of a string, and in the string \ also has a special meaning and itself needs to be escaped. So if you match a "\d", the string is written as "\\d", then the regular will be written

"\\\\d", so it's too much trouble. At this time we use the concept of ' r\d ', and the regular is ' r\\d ' on it.

Greedy match

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

Several commonly used non-greedy matching pattern

*Repeat any number of times, but with as few repetitions as possible + repeat1 or more times, but with as few repetitions as possible? Repeat 0 or 1 times, but repeat {n,m} as little as possible. Repeat N to M, but repeat {n,} as little as possible, but repeat more than n times.

. * ? Usage of

is any character * to take 0 to infinity length? non-greedy mode. Where together is to take as few as possible any character, generally do not write so alone, he mostly used in:. *X is the character of any length preceding it, until an X appears

All right,  let's write about this today, and   tomorrow.  






Python-Modular path Regular expression

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.