Python Learning notes (17)

Source: Internet
Author: User

Introduction to Regular expressions

The regular expression is a powerful weapon to match string data, the design idea is to use a descriptive language to define a rule for the string, and if it matches the description rule of the string, it is considered to be a match, and if it does not conform to the string description rule, it is considered illegal.


Ii. basic knowledge of regular expressions

\d: Digital
\w: Letters or numbers
\s: whitespace character
. : matches any one character
*: matches any character of any length
+: At least one string
? : represents 0 or 1 characters
{n}: represents n characters
{N,m}: represents n-m characters, at least N, up to m, including M
^: Represents the beginning of a line, ^\d means that it must start with a number
$: Indicates the end of the line, ^d$ indicates that it must end with a number
  


Third, the regular expression of the advanced

\D{3}: Match three digits
\d{3,8}: Match 3-8 digits
[0-9a-za-z\_]: match A numeric letter the latter underline
[0-9a-za-z\_]+: Matches a string consisting of at least one number, letter, or underscore
[0-9a-za-z\_]*: Matches a string consisting of at least one number, letter, or underscore
a| B: Match A or b


Iv. Common Regular Expressions

Http://www.jb51.net/article/76901.htm


Five, re module

The RE module is provided in Python for support of regular expressions, the escape character is provided in Python, but R is also provided, and by using R "Ab\r" there is no need to escape the associated keywords in the string.

# Introducing the RE module
Import re

# Determines whether a string matches related regular expressions
Re.match (R ' Regular expression ', detection text to be matched) = True/false (return value)

# Slicing strings
Re.split (R ' [\s\,+] ', ' A, B, C, d ') = List

# string grouping, providing string substring
# Use () in regular expressions to indicate the grouping to extract
m = Re.match (R ' Group regular expression ', string)
M.group (0) #group (0) is always the original string
M.group (1) #第一个分组子串

# greedy Match
# The regular expression defaults to a greedy match and can change the regular expression to a non-greedy match in a certain way


Vi. Compiling regular expressions

Regular expressions are compiled in the Python language to compile the regular expression and then to match the string, but if you need to match the string too many times, each time you need to match the regular expression, then the very good princess space
You can pre-compile the regular expression and then make a match

Import re
# Create a regular expression object that passes after
re_compiled = Re.compile (R ' Regular expression ')
# You can directly match a string by using the regular expression in the following way, because the original information of the regular expression is already contained in the object of the compiled regular expression

  

  

  

Python Learning notes (17)

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.