Python Basics-Day 5 learning note-Standard library of modules: RE (14) Regular expressions

Source: Internet
Author: User
Tags printable characters

RE Module Introduction

Regular Expressions (RE) are used as a basis for processing files and data, for advanced text pattern matching, and for features such as search-and-replace. The essence is a string of characters and special symbols (metacharacters: metacharacter) that describe some repetition of these characters and characters, so that they can match a set of strings with similar characteristics in a pattern, or they can match a series of strings with similar characteristics according to a pattern. We call pattern matching (Patten match). In Python, Pattern-match has two main ways of doing it: Search and match. Search, search for matching patterns in any part of the string, match, determine whether a string can match a pattern in whole or in part from the beginning.

Meta Character Descriptor metacharacter
Symbol Describe Regular expressions Matched string
Use Select one to match multiple regular expression patterns, also known as union or Logical or
Re1| Re2 Match Regular expression Re1 or re2 Bat|bet|bit BAT, bet, bit
Match any single character
.

Matches any character (except \ n),

Includes letters, numbers, spaces (not including "\ n"), printable, and non-printable characters.

Display the period symbol itself, you need to use "\."

F.o Match any character before the letter "F" and "o"; for example Fao,f9o,f#o
.. Any of two characters
. end Matches any one character before the string "End"
Match from start or end of string or word boundary
^ or \a Match string Start part ^dear Any string with dear as the starting point
$ or \z Match string Termination section /bin/tesh$ Any string ending with/bin/tesh
^subject:hi$ Any string consisting of a separate string Subject:hi
\bbb Match the boundaries of a word \bthe Any string starting with the
\bthe\b Just match the word the
\b Match the pattern that appears in the middle of a word \bthe Any string that contains, but does not start with, the
Creating character sets , scoping and negation
[...] Match any single character from a character set B[aeiu]t BAT, bet, bit, but
[CR] [23] [DP] [O2] A string containing four characters, such as C2DO\R3P2, etc.
[.. X-y:] Matches any single character in the X~y range Z.[0-9] The letter "Z" followed by any character followed by a number
[^...] does not match any one of the characters appearing in this character set, including a range of characters (if present in this character set) [^aeiou] A non-vowel character
[^\t\n] Do not match tabs or \ n
Using the closure operator for existence and frequency matching
* Matches 0 or more occurrences of the preceding regular expression [a-za-z0-9]*
+ Matches 1 or more occurrences of the preceding regular expression [a-z]+\.com
Matches a regular expression that appears before 0 or 1 times [Dn]ot? T appears once or does not appear, i.e. Do,no,dot,not
{} {n} exactly matches the regular expression preceded by n times; {M,n} matches the regular expression that appears before the m~n-th [0-9] {15,16} Match 15 or 16 digits (such as a credit card number)
</? [^>]+> Match all valid (and invalid) HTML tags
A special character that represents a character set
\d Match any decimal number equal to [0-9] \D{3}-\D{3}-\D{4} Format of US phone numbers
\d does not match any non-numeric number, equals [^0-9]
\w Matches any alphanumeric character equal to [a-za-z0-9] \[email protected]\w+\.com Simple e-mail address in [email protected] Format
\w Does not match any alphanumeric number, equals [^a-za-z0-9]
\s Match any space character equal to [\n\t\r\v\f] (\s opposite)
Use parentheses to specify grouping
(...) Match the closed regular expression, and then save as a subgroup; Use groups to embody \d+ (\.\d*)? A string representing a simple floating-point number; in other words, any decimal digit, followed by a decimal point and 0 or more decimal digits.
Extended notation
(?...) Provide a tag before the match is judged to achieve a pre-view (or post-visual) match or condition check
(? P<name>, ...)
Common operations

Re.match ()

Re.group ()

.

Python Basics-Day 5 learning note-Standard library of modules: RE (14) Regular expressions

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.