Python学习笔记——正则表达式入门

# 本文对正则知识不做详细解释,仅作入门级的正则知识目录。

  正则表达式的强大早有耳闻,大一时参加一次选拔考试,题目就是用做个HTML解析器,正则的优势表现得淋漓尽致。题外话不多讲,直接上干货:

1. 元字符:

   aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAARQAAADTCAIAAADPiHuWAAAUPklEQVR4nO2dQXLjOAxFdSBX9XG8mrt42/fwAfoUPsGcQ8ssUkk8C3c8Mgl8fkCUI8n/VVdKpkAQJPFFWZLVw1UIkWL46QCE2CoSjxBJJB4hkkg8QiR5CfEMwzAMz+jpcg3VnhftFO/8yYGtitfo5LPyzNuu7TFMF3Cncq3w/tOB7YkddpLJm74TfPdWbwBj5mO0MA0/aHW0U/78+bPoUK+KffaqgJ+/YRh+//4dcptLOPKj2cotwr4JSnqYmt23m4V7Zefdu9GcyH///bdIzS5uTbOceKYff//+3awVYo5y7h/va06vqNbPPvtWH5WZ4/QtKUnxFIdYk1+/fnli4GOrMxKLh/GP22qaeUPRLNwZO+/eDXIiQ+JpOh+G4devX017/NEzwDberkRCM1Uknj3zBPGYLCeeK62KOeIJKQeMw15VtLde4SnEk5oTT916yIYUjxl2kbjN5kJ5zPfFNNuxZu7svXvWF1mPhHii+kxUKUqWEA8TUh3eUI0t09ae2HP3hsmJEzOvafGQH3NVEj5DdT2w8X2v14TEs2Fukzf91lEfKQteTTykPDASz96466T4yo5n9Caef/75Z1p4u9xsfvWvHU5TxxPq08STc0XubZpJPNtj+OY6OW2bTuTU4M70JumNP3/+3HblxFPv8gpJ8RRE3ZJtNT3wZhLPthmc25RXeJloZovYJ8jywVmssD6ZKvUuoEPglu8RX33T7Ll7Cymk2WJuL2PAVOnSZd4JODbNjGH97L+HQiyExCNEEolHiCR/xXMSQnAY4hmfyJObE6IXEo8QSSjx3K423rcPh0NRODOC+U5elnoWes2LaJIXz8jNU30nrqgi8UQpxhB/xE7AvIgmPcXDzIfE04ViRsztwriYlJzkxJS2eOpRvovHA8yEx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NqQYpJMucOI5fw7H99vKczl9HSb/m3yXCIqSkHiYpCEVaFYxs7apxqh4Qk2A7kR7J+ZAied8vB7Pt9O2t9PhujbxmH89h9PC0NEaB0A6N1sJiYc/TKTjEST0ynP4uKzyO09aPJ6rqAHjPKRJUhXFyCT0SYYnPNjvPOfjdRiuGxJP9MjKKydx/I6Kpy6fme4SzxIErrbdrhl0109OPEWGFYIBFU0YA8/GXBMYgL3XTTLaZpASTxeCl6pPH4fe/8vIfPGYqQByncdziFM5WgJSuRaPGSQWJ9Nrc6wEJnyf53L6Go7vfSMoSuqMqT96NmNLPCbAuBnMouJh7JmSonohGIknByOe7ytstwsGt8vWXSMoSnBe1suCdzDmY2CMQ+JhaHrmI2TkNC1PDJGoIcXzdbr8XXnOxyddqianll95mOUl1BAOgC/JiQcfKbAToHzBQ562vR9vjxcMnZUz6tVT8IJB7cG0aXYWrHVei6LJSp9t4898vHJzV926pzTPslddsLfOckYqeBCYvkhCUdYonhBMZkQ98GZz6pLMEfYS8Yg7a/xJghCbYHXi4c/ZxBI8c+q3zurEI8RWkHiESGKLRwjBUIpHCBFF4hEiyX+F2BHpqPXoFAAAAABJRU5ErkJggg==" alt="" />

  与之对应的还有反义字符,大部分为小写字母变大写,例如\D表示匹配非数字的字符。

2. 重复(即匹配变长的字符串):

  元字符可匹配单一的字符类型,若想匹配长度未知或长度限定的字符串需要在后边加上限定符。

  aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAN8AAAC6CAIAAAChog+SAAAPZElEQVR4nO2dTXLkKBCFOZAi5jha9V207XvoAH2KOkGfg6UXDts1C7VlCjKTx0+VEilfTMRIKIEEPlIqJNrubjJplTvagVjOOede4dXzKkpLfmqj8MJf7Fi71Hn2soHkjlN7WUgThEaFRf358wevBSy/2jENOtgzZPj79uBeWnogGCOnpYn7pd+/f28HO51yXrzTUm9DcZOB7rgjpMiVTUgH/fr1a+/K//77Dy+2bkTBU7KWnTyhit+/f++JXJmlXRSZkeULlSqROp+yPbW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17jXV4V0tuwY5nT0F58gndkFkcbhBxdfkFqe4d4hKqOzeccwpyPfv5PtrVif67LCtxdVcUk2Oz2dz/vC6Mhvl5DYmR3ajmMvR74WOqtny1EyOnXtGA6NyeM6qgYNpUanoh3DAkN1dHK5hkDTG51ezY5hOXsLnY1x90CV0NmyY3j7Yb58bpb/vlYOshy4k1igM3vM2fPp+R3DZHUxYevs3LQs81bCv8Iei4oyCh6qFUxn847hdd6M35bpPq/Rxo8j15VeHTtzO4Y9dotf562Q2zJ9//+xJKGQUVR0Z2/aMfy9GrVvGg5LO3Inca/YiVz1PrOviCuEonPLeFumjfR/CdwUSkOpfjU8d/alc/znTiHLgxFPp5AXpzMy848pRiet4egMhQyqELEe9OTYSZaAx35VegKdzO62segUAmdKIRnkPMdBFZ3pJaMzVv2O4e2n/fSxLl/hz/ZvM3VvMiMVjaj4AJDZMRyVQM6En2KmZV2mrYDvhFvqMBTRVarwK5CWHcNtZT5P7e+KooxCiiCh5N3Aw2CRsZw00Kz+X9DZjuE6G/+Et/lZr4pKe73s+077+livjM6r7xg+qtsRGZ0WO/XK6DQ69Yqm02RSophOk0mnjE6TXhmdJr0yOk169T8XQ2gd/49FrAAAAABJRU5ErkJggg==" alt="" />

3. 范围与分组:

  有时元字符并不能满足匹配字符的需求,这时就需要[]来圈定匹配范围,例如要匹配一个只带有a,s,d,f的单词,可以用\b[asdf]\b;正则里面还有小括号用来表示分组,比如你想重复某几个字符,就可以使用分组,例如(\d.\d){1,3}表示匹配小括号里的重复内容一到三次。此外,小括号还可以用来后向引用等,此处不记。

4. 零宽断言:

  零宽断言主要用来查找在某些断言(指定内容)之前或之后的内容,例如(?=exp)匹配表达式exp前面出现的内容(不匹配exp),(?<=exp)匹配表达式exp后面出现的内容,还有负向零宽断言等此处不记。

5. 贪婪与懒惰:

  当正则表达式中包含能接受重复的限定符时,通常的行为是(在使整个表达式能得到匹配的前提下)匹配尽可能多的字符,即贪婪匹配。如表达式a.*b,搜索aabab的话,它会匹配整个字符串aabab。而懒惰匹配会匹配尽可能少的字符串,只需要在限定符后面加一个?,例如a.*?b会匹配aab(第一到第三个字符)和ab(第四到第五个字符)。

6. Python的re模块:

  python 提供re模块,提供正则的所有功能。下面只记两个方法和一个注意的地方。

  6.1 match方法:

    match()方法判断是否匹配,如果匹配成功,返回一个Match对象,否则返回None

 >>> import re
>>> re.match(r'^\d{3}\-\d{3,8}$', '010-12345')
<_sre.SRE_Match object at 0x1026e18b8>
>>> re.match(r'^\d{3}\-\d{3,8}$', '010 12345')
>>>

    如果正则表达式中定义了组,就可以在Match对象上用group()方法提取出子串来。注意到group(0)永远是原始字符串,group(1)group(2)……表示第1、2、……个子串。使用groups()方法还可以得到所有子串。

  6.2 split方法:

    split()方法可切分子串,如下:

 >>> re.split(r'[\s\,\;]+', 'a,b;; c  d')
['a', 'b', 'c', 'd']

  6.3 关于转义字符:

    python转义字符串也用\表示,所以正则表达式读取时作为字符串出现时会被编译器自动去掉转移字符的\,正则使用时就会出错,所以推荐使用python的 r 前缀。

ps:

  本文是对正则表达式的学习总结,大部分知识来源于网上。下面给出两个个人认为比较好的链接:

  1.http://www.jb51.net/tools/zhengze.html 对正则表达式介绍的比较详细,个人认为要想深入学习正则表达式还是买本书比较好。

  2.http://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000/001386832260566c26442c671fa489ebc6fe85badda25cd000 来自廖雪峰大神的教程,结合python一起讲,只是稍微短了一点。

上一篇:I.MX6 bootargs 内核参数设定


下一篇:nssstring 转换大小写