当前所在位置: 首页 > 儿童教育 > 正文

python正则表达式语法学习笔记

2022-11-15 canyinms.com 【 字体:

正则表达式(regular expression)描述了一种字符串匹配的模式(pattern),可以用来检查一个串是否含有某种子串、将匹配的子串替换或者从某个串中取出符合某个条件的子串等。

Python 自1.5版本起增加了re 模块,它提供 Perl 风格的正则表达式模式。

re 模块使 Python 语言拥有全部的正则表达式功能。

compile 函数根据一个模式字符串和可选的标志参数生成一个正则表达式对象。该对象拥有一系列方法用于正则表达式匹配和替换。

本文重点给大家介绍python 正则表达式语法。

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).    (?

The special sequences consist of "\\" and a character from the listbelow.  If the ordinary character is not on the list, then theresulting RE will match the second character.    \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.

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 RegexObject.    purge     Clear the regular expression cache.    escape    Backslash all non-alphanumerics in a string.

Some of the functions in this module takes flags as optional parameters:    A  ASCII       For string patterns, make \w, \W, \b, \B, \d, \D                   match the corresponding ASCII character categories                   (rather than the whole Unicode categories, which is the                   default).                   For bytes patterns, this flag is the only available                   behaviour and needn't be specified.    I  IGNORECASE  Perform case-insensitive matching.    L  LOCALE      Make \w, \W, \b, \B, dependent on the current locale.    M  MULTILINE   "^" matches the beginning of lines (after a newline)                   as well as the string.                   "$" matches the end of lines (before a newline) as well                   as the end of the string.    S  DOTALL      "." matches any character at all, including the newline.    X  VERBOSE     Ignore whitespace and comments for nicer looking RE's.    U  UNICODE     For compatibility only. Ignored for string patterns (it                   is the default), and forbidden for bytes patterns.

 

下面看下正则表达式匹配的流程:

正则表达式的大致匹配过程是:依次拿出表达式和文本中的字符比较,如果每一个字符都能匹配,则匹配成功;一旦有匹配不成功的字符则匹配失败。如果表达式中有量词或边界,这个过程会稍微有一些不同,但也是很好理解的,自己多使用几次就能明白。

总结

到此这篇关于python 正则表达式语法记录的文章就介绍到这了,希望大家以后多多支持好二三四!

阅读全文
相关推荐

橘子洲旅游攻略 长沙橘子洲怎么玩

橘子洲旅游攻略 长沙橘子洲怎么玩
橘子洲是湘江中的一个冲击沙洲,四面环水绵延数十里,是国家重点风景名胜区。景区内有历史文化陈列馆、潇湘名人会所、柑橘文化园等景观,集观赏休闲娱乐于一体。在特定时间段,橘子洲烟火是一场绝不可错过的视觉盛宴。景区原面积约17公顷,其中开放区面积约6公顷,主要景点有洲头颂橘亭、汉白玉诗词碑、铜像广场、藤架广场、揽岳亭、枕江亭、盆景园、大门广场等。

西安周边旅游攻略 西安周边最值得去的地方

西安周边旅游攻略 西安周边最值得去的地方
1、华山风景名胜区:“华山论剑谁为峰,一见重阳道成空”,武侠小说为华山披上了浓厚的江湖色彩,同时华山用它的险峻向每一位前来攀登的游客发起挑战。整个华山拥有多达200余处景点,东峰的日出、西峰的峭壁、南峰的林木、北峰的云雾,每一处景色都独具魅力。打卡建议:在西峰乘坐索道上山,再从北峰乘坐索道下山,一天游玩下来大致需要6—8个小时,并且可以把景点都逛到。如果选择夜爬华山看日出的话,记得带一件厚一点的衣服,越往上爬山上越冷。还有记得爬山戴双手套,因为华山有些路段需要双脚并用才能攀登的上去。

毛坦厂中学万人雨中送考 毛坦厂送考头车车牌91666祝福考生

毛坦厂中学万人雨中送考 毛坦厂送考头车车牌91666祝福考生
六安市毛坦厂中学,简称“毛中”,位于安徽省六安市金安区毛坦厂镇,被称为“亚洲最大高考工厂”。每次送考时节也都会有万余名家长和当地居民前来送考,毛坦厂镇的万人送考成为当地乃至安徽省内的一个“高考景观”。  毛坦厂中学万人雨中送考  6月5日,安徽六安。今早7点半,毛坦厂中学送考车队又一次出发,考生

珠峰被救女子不愿支付全部救援费用 珠峰攀登者为救遇险女子放弃登顶

珠峰被救女子不愿支付全部救援费用 珠峰攀登者为救遇险女子放弃登顶
近日,湖南省登山队2名队员范江涛、谢如祥在距离登顶珠峰不到400米时,为救一名同中国籍的女子放弃登顶。此事引起网友广泛关注,网友纷纷为两人的义举支持点赞。但在成功救人后,被救女子的态度和表现,让两人倍感寒心。为请求救女子,他们当时允诺给夏尔巴向导1万美元,然而该女子事后只愿承担4000美元。

​登珠峰获救女子只愿承担4成救援费是怎么回事 登珠峰获救女子只愿承担4成救援费具体情况

​登珠峰获救女子只愿承担4成救援费是怎么回事 登珠峰获救女子只愿承担4成救援费具体情况
近日,湖南省登山队2名队员为救人放弃珠峰登顶计划引起热议。3日,有网友发布消息称:被救者只愿承担4成救援费。具体登珠峰获救女子只愿承担4成救援费是怎么回事?一起来了解登珠峰获救女子只愿承担4成救援费具体情况。登珠峰获救女子只愿承担4成救援费是怎么回事近日,湖南省登山队2名队员在距离登顶珠峰不到400

国内出现偏肺病毒感染 尚无治疗药物

国内出现偏肺病毒感染 尚无治疗药物
5月31日,据美国疾控中心(CDC)的呼吸道病毒监测系统显示,今年春季以来,人类偏肺病毒(HMPV)在美国各地区呈现高发态势,该病毒正肆虐美国各大医院的重症监护病房和儿科医院。  6月3日,某三甲医院感染控制中心主任医师告诉记者:“根据近几年国内零散报告,人偏肺病毒在呼吸道感染病人中确实是存在的

男子连吃100天麦当劳减重53斤是真的吗 男子连吃100天麦当劳为什么能减重53斤

男子连吃100天麦当劳减重53斤是真的吗 男子连吃100天麦当劳为什么能减重53斤
近日,据说美国一57岁男子通过连吃100天麦当劳的方式减肥,减了53斤。那么,男子连吃100天麦当劳减重53斤是真的吗?到底男子连吃100天麦当劳为什么能减重53斤?一起来了解具体情况。男子连吃100天麦当劳减重53斤是真的吗近日,据外媒报道,美国一名57岁男子通过连吃100天麦当劳的方式减肥,体重

本周狗屎运最旺的星座 本周狗屎运最旺的四大星座

本周狗屎运最旺的星座 本周狗屎运最旺的四大星座
本周(6.5-6.11)狗屎运最旺的星座TOP4如下:  TOP4:巨蟹座  金星冲冥王,巨蟹座走的狗屎运,可以说是超级大坨!不光是财运一飞冲天,主要事业运+学业运,迎来双运加速的爆发期!  TOP3:摩羯座  月亮六合水星,紧接着拱水星,摩羯运势仿佛踩上了蹦蹦床!正财运和偏财运,简直装备了跃迁

印度耗资百亿卢比的大桥又塌了是什么情况 印度耗资百亿卢比的大桥又塌了是为什么

印度耗资百亿卢比的大桥又塌了是什么情况 印度耗资百亿卢比的大桥又塌了是为什么
据印度媒体当地时间6月4日报道,印度比哈尔邦在建的横跨恒河的阿格瓦尼-苏丹甘吉大桥再次发生部分坍塌。据悉,此桥耗资百亿卢比。具体印度耗资百亿卢比的大桥又塌了是什么情况?到底印度耗资百亿卢比的大桥又塌了是为什么?一起来了解。印度耗资百亿卢比的大桥又塌了是什么情况据印度媒体6月5日报道,上周日下午6点左

一个村考出300多大学生6个北大 湖南一小村考上一本奖4000二本3000

一个村考出300多大学生6个北大 湖南一小村考上一本奖4000二本3000
一个村考出300多大学生6个北大是怎么回事?湖南一小村考上一本奖4000二本3000是什么情况?湖南益阳,爱屋湾村:一个让人自豪的地方,也是一个让人感动的地方。这个小村庄,历经数十年积淀,已经培养出了不少人才。据统计,爱屋湾村十年来累计奖励学生68名,其中有6人考上了北京大学,5人留学哈佛,其他
本文Tag