Home >Backend Development >Python Tutorial >Regular expressions and the use of Python Re library

Regular expressions and the use of Python Re library

不言
不言Original
2018-04-04 15:19:332777browse

The content of this article is about regular expressions and the use of Python Re library. Now I share it with everyone. Friends in need can refer to the content of this article


Regular expressions and the use of Python Re library


##Note: Pictures and related codes are sourced from MOOC


Regular Expressions

Common operators of regular expressions

Regular expressions and the use of Python Re library

Regular expressions and the use of Python Re library

Regular expression syntax examples

Regular expressions and the use of Python Re library

Regular expressions and the use of Python Re library

Regular expression in the form of IP address string

Regular expressions and the use of Python Re library

Usage of Python Re library

Regular expression Expression type

Regular expressions and the use of Python Re library

Regular expressions and the use of Python Re library

When the regular expression contains escape characters, use raw string

Main functions of Re library

Regular expressions and the use of Python Re library

Example

import re
match = re.match(r'[1 - 9]\d{5}','BIT 100081')
print(match.group(0))# match 函数从一个字符串的开始位置起匹配正则表达式,所以变量 match 应该等于 # None,所以上述代码会抛异常,正确的写法是先对 match 进行判断

Another equivalent usage of Re library

Regular expressions and the use of Python Re library

pat is a regular expression object compiled from the regular expression string form. You can directly use the above 6 functions of the Re library.

Re library match object

Attributes of match object

Regular expressions and the use of Python Re library

Greedy matching and minimum matching of Re library

Re library uses greedy matching by default, that is, output Match the longest string

Related recommendations:

Usage and difference between greedy mode and non-greedy mode in Python regular expressions

videocapture library produces python video high-speed transmission program

The above is the detailed content of Regular expressions and the use of Python Re library. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn