Python:正则表达式—— re 模块

一、什么是正则表达式(Regular Expression)

正则表达式本身是一种小型的、高度专业化的编程语言,它内嵌在Python中,并通过 re(regular expression)模块实现。使用这个小型语言,你可以为想要匹配的相应字符串集指定规则。

二、正则表达式的元字符

1. 什么是元字符

元字符是一些在正则表达式中有特殊用途、不代表它本身字符意义的一组字符。

2. Python中的元字符

(1).  :匹配除“ ”之外的任何单个字符。要匹配包括 ' ' 在内的任何字符,请使用像"(.| )"的模式

(2)

1 def sub(pattern, repl, string, count=0, flags=0):
2     """Return the string obtained by replacing the leftmost
3     non-overlapping occurrences of the pattern in string by the
4     replacement repl.  repl can be either a string or a callable;
5     if a string, backslash escapes in it are processed.  If it is
6     a callable, it's passed the match object and must return
7     a replacement string to be used."""
8     return _compile(pattern, flags).sub(repl, string, count)
原文地址:https://www.cnblogs.com/huiAlex/p/8028467.html