0
点赞
收藏
分享

微信扫一扫

正则表达式的学习(2)一些干货


标题正则表达式的学习(2)一些干货

在Python这门语言中完全支持正则表达式,正则作为一个工具毋庸置疑,功能是十分强大的,那么,在深入学习正则前,我想,看一看Python的库re是一个比较好的主意。
首先,打开IDE pycharm,新建任意一个Python文件,导入re库。`按住ctrl键点击re这个字母,打开一个名称为re.py的文件。

#Secret Labs' Regular Expression Engine
#re-compatible interface for the sre matching engine
# Copyright (c) 1998-2001 by Secret Labs AB. All rights reserved.
#
# This version of the SRE library can be redistributed under CNRI's
# Python 1.6 license. For any other use, please contact Secret Labs
# AB (info@pythonware.com

以上是源码的开始几行注释,意思是这个正则表达式的引擎是由一个名字叫秘密实验室的工作组AB开发的,从Python1.6开始提供支持,如有其它用途,请联系AB,然后一个邮箱。
第十七行,问题来了。

r"""Support for regular expressions (RE).
#这个意思是正则支持r‘’字符串,也就是原生字符串类型,
#说直白点,意思就是使用r“string”,string内的转义字符不进行
#转义,例如:a=r'\\abc//\[abc]//\\{}'和a='\\abc//\#[abc]//\\{}' 是两个不同的字符串,推荐使用加r
下面是解释re的使用范围包括编码问题,
This module provides regular expression matching operations similar to
those found in Perl. It supports both 8-bit and Unicode strings; both
the pattern and the strings being processed can contain null bytes and
characters outside the US ASCII range.
意思可以用在perl,也支持 8-bit和Unicode编码以及空字符串和us ASCII
下面是解释re可以支持的特殊字符以及大部分普通字符
Regular expressions can contain both special and ordinary characters.
Most ordinary characters, like "A", "a", or "0", are the simplest
regular expressions; they simply match themselves. You can
concatenate ordinary characters, so last matches the string 'last'.
举例如,“A”,"a",或者“0”,这是最简单的正则表达式
下面介绍正则中的特殊字符
The special characters are:
"." Matches any character except a newline.
"^" Matches the start of the string.
"$" Matches the end of the string or just before the newline at
the end of the string.
"*" Matches 0 or more (greedy) repetitions of the preceding RE.
Greedy means that it will match as many repetitions as possible.
"+" Matches 1 or more (greedy) repetitions of the preceding RE.
"?" Matches 0 or 1 (greedy) of the preceding RE.
*?,+?,?? Non-greedy versions of the previous three special characters.
{m,n} Matches from m to n repetitions of the preceding RE.
{m,n}? Non-greedy version of the above.
"\\" Either escapes special characters or signals a special sequence.
[] Indicates a set of characters.
A "^" as the first character indicates a complementing set.
"|" A|B, creates an RE that will match either A or B.
(...) Matches the RE inside the parentheses.
The contents can be retrieved or matched later in the string.
(?aiLmsux) Set the A, I, L, M, S, U, or X flag for the RE (see below).
(?:...) Non-grouping version of regular parentheses.
(?P<name>...) The substring matched by the group is accessible by name.
(?P=name) Matches the text matched earlier by the group named name.
(?#...) A comment; ignored.
(?=...) Matches if ... matches next, but doesn't consume the string.
(?!...) Matches if ... doesn't match next.
(?<=...) Matches if preceded by ... (must be fixed length).
(?<!...) Matches if not preceded by ... (must be fixed length).
(?(id/name)yes|no) Matches yes pattern if the group with id/name matched,
the (optional) no pattern otherwise.
也不用看这些源码了,只要知道一些常用的特殊字符,勉强够用了,比如
‘.’ ,"*" ,"?" ,"^" , "$" "+"
{m,n} "|" [] (...)
以上特殊字符高频使用,正则难点在于组合使用这些特殊字符以及
一些普通字符。***(详细分析下篇文章见)***

下面介绍下有哪些普通字符。 \number Matches the contents of the group of the same number.
\A Matches only at the start of the string.
\Z Matches only at the end of the string.
\b Matches the empty string, but only at the start or end of a word.
\B Matches the empty string, but not at the start or end of a word.
\d Matches any decimal digit; equivalent to the set [0-9] in
bytes patterns or string patterns with the ASCII flag.
In string patterns without the ASCII flag, it will match the whole
range of Unicode digits.
\D Matches any non-digit character; equivalent to [^\d].
\s Matches any whitespace character; equivalent to [ \t\n\r\f\v] in
bytes patterns or string patterns with the ASCII flag.
In string patterns without the ASCII flag, it will match the whole
range of Unicode whitespace characters.
\S Matches any non-whitespace character; equivalent to [^\s].
\w Matches any alphanumeric character; equivalent to [a-zA-Z0-9_]
in bytes patterns or string patterns with the ASCII flag.
In string patterns without the ASCII flag, it will match the
range of Unicode alphanumeric characters (letters plus digits
plus underscore).
With LOCALE, it will match the set [0-9_] plus characters defined
as letters for the current locale.
\W Matches the complement of \w.
\\ Matches a literal backslash.
极高频率出现的普通字符有\w,\W,\d,\D\,\s,\S,\\,这七个字符必须掌握,\A,\Z,\b不常用.*(详细分析下篇文章见)*
下面是常用的这个re库内的方法。
This module exports the following functions:
match Match a regular expression pattern to the beginning of a string.
fullmatch Match a regular expression pattern to all of a string.
search Search a string for the presence of a pattern.
sub Substitute occurrences of a pattern found in a string.
subn Same as sub, but also return the number of substitutions made.
split Split a string by the occurrences of a pattern.
findall Find all occurrences of a pattern in a string.
finditer Return an iterator yielding a Match object for each match.
compile Compile a pattern into a Pattern object.
purge Clear the regular expression cache.
escape Backslash all non-alphanumerics in a string.
极为常用的几个方法为,splite compile,findall,
sub,search,match***(详细分析下篇文章见)***


举报

相关推荐

0 条评论