正则表达式的学习(2)一些干货
发布日期:2021-05-07 15:03:44 浏览次数:25 分类:精选文章

本文共 2988 字,大约阅读时间需要 9 分钟。

标题正则表达式的学习(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 tothose found in Perl.  It supports both 8-bit and Unicode strings; boththe pattern and the strings being processed can contain null bytes andcharacters 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 simplestregular expressions; they simply match themselves.  You canconcatenate 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
...) 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). (?
上一篇:正则表达式(3)更多干货--特殊字符与普通字符
下一篇:Linux下的一些文本处理工具

发表评论

最新留言

路过,博主的博客真漂亮。。
[***.116.15.85]2025年04月05日 19时06分55秒