[{"data":1,"prerenderedAt":2551},["ShallowReactive",2],{"post-2025-07-11-python":3},{"_path":4,"_dir":5,"_draft":6,"_partial":6,"_locale":7,"title":8,"description":9,"date":10,"tags":11,"body":14,"_type":2545,"_id":2546,"_source":2547,"_file":2548,"_stem":2549,"_extension":2550},"\u002Fblog\u002F2025-07-11-python","blog",false,"","Python-使用正则表达式","Python正则表达式：re模块、匹配模式、分组、替换与常用正则技巧。","2025-07-11",[12,13],"Python","基础",{"type":15,"children":16,"toc":2525},"root",[17,22,29,75,81,102,122,158,172,177,191,196,230,244,317,325,520,525,537,551,556,564,730,735,740,791,815,820,846,860,865,870,875,888,902,907,912,925,939,965,979,987,992,1030,1044,1049,1076,1082,1087,1101,1106,1120,1125,1139,1144,1158,1174,1179,1229,1239,1253,1258,1267,1291,1297,1307,1321,1326,1364,1456,1461,1536,1541,1549,1554,1619,1698,1702,1710,1716,1740,1754,1758,1789,1794,1860,1864,1872,1877,1883,1912,1926,1930,1960,1965,2033,2037,2045,2050,2096,2101,2109,2121,2175,2179,2202,2207,2224,2238,2242,2295,2300,2346,2350,2358,2363,2398,2412,2416,2457,2462,2507,2511,2519],{"type":18,"tag":19,"props":20,"children":21},"element","hr",{},[],{"type":18,"tag":23,"props":24,"children":26},"h2",{"id":25},"正则表达式语法",[27],{"type":28,"value":25},"text",{"type":18,"tag":30,"props":31,"children":32},"p",{},[33,35,41,43,48,50,57,59,65,67,73],{"type":28,"value":34},"在处理字符串时，经常会有",{"type":18,"tag":36,"props":37,"children":38},"strong",{},[39],{"type":28,"value":40},"查找符合某些复杂规则的字符串",{"type":28,"value":42},"的需求。",{"type":18,"tag":36,"props":44,"children":45},{},[46],{"type":28,"value":47},"正则表达式",{"type":28,"value":49},"就是用于描述这些规则的工具。换句话说，正则表达式就是记录文本规则的代码。对于接触过DOS的用户来说，如果想匹配当前文件夹下所有的文本文件，可以输入",{"type":18,"tag":51,"props":52,"children":54},"code",{"className":53},[],[55],{"type":28,"value":56},"dir *.txt",{"type":28,"value":58},"命令，按",{"type":18,"tag":51,"props":60,"children":62},{"className":61},[],[63],{"type":28,"value":64},"Enter",{"type":28,"value":66},"键后，所有“.txt”文件将会被列出来。这里的",{"type":18,"tag":51,"props":68,"children":70},{"className":69},[],[71],{"type":28,"value":72},"*.txt",{"type":28,"value":74},"即可理解为一个简单的正则表达式。",{"type":18,"tag":76,"props":77,"children":79},"h3",{"id":78},"行定位符",[80],{"type":28,"value":78},{"type":18,"tag":30,"props":82,"children":83},{},[84,86,92,94,100],{"type":28,"value":85},"行定位符就是用来描述字符串的边界，",{"type":18,"tag":51,"props":87,"children":89},{"className":88},[],[90],{"type":28,"value":91},"^",{"type":28,"value":93},"表示行的开始，",{"type":18,"tag":51,"props":95,"children":97},{"className":96},[],[98],{"type":28,"value":99},"$",{"type":28,"value":101},"表示行的结尾。如：",{"type":18,"tag":103,"props":104,"children":108},"pre",{"code":105,"language":106,"meta":7,"className":107,"style":7},"^tm\n","python","language-python shiki shiki-themes github-dark",[109],{"type":18,"tag":51,"props":110,"children":111},{"__ignoreMap":7},[112],{"type":18,"tag":113,"props":114,"children":117},"span",{"class":115,"line":116},"line",1,[118],{"type":18,"tag":113,"props":119,"children":120},{},[121],{"type":28,"value":105},{"type":18,"tag":30,"props":123,"children":124},{},[125,127,133,135,141,143,148,150,156],{"type":28,"value":126},"该表达式表示要匹配字符串",{"type":18,"tag":51,"props":128,"children":130},{"className":129},[],[131],{"type":28,"value":132},"tm",{"type":28,"value":134},"的开始位置是行头，如",{"type":18,"tag":51,"props":136,"children":138},{"className":137},[],[139],{"type":28,"value":140},"tm equal Tomorrow Moon",{"type":28,"value":142},"可以匹配到",{"type":18,"tag":51,"props":144,"children":146},{"className":145},[],[147],{"type":28,"value":132},{"type":28,"value":149},"，而 ",{"type":18,"tag":51,"props":151,"children":153},{"className":152},[],[154],{"type":28,"value":155},"Tomorrow Moon equal tm",{"type":28,"value":157},"则不匹配。但如果使用：",{"type":18,"tag":103,"props":159,"children":161},{"code":160,"language":106,"meta":7,"className":107,"style":7},"tm$\n",[162],{"type":18,"tag":51,"props":163,"children":164},{"__ignoreMap":7},[165],{"type":18,"tag":113,"props":166,"children":167},{"class":115,"line":116},[168],{"type":18,"tag":113,"props":169,"children":170},{},[171],{"type":28,"value":160},{"type":18,"tag":30,"props":173,"children":174},{},[175],{"type":28,"value":176},"后者可以匹配而前者不能匹配。如果要匹配的字符串可以出现在字符串的任意部分，那么可以直接写成下面的格式，这样两个字符串就都可以匹配了。",{"type":18,"tag":103,"props":178,"children":180},{"code":179,"language":106,"meta":7,"className":107,"style":7},"tm\n",[181],{"type":18,"tag":51,"props":182,"children":183},{"__ignoreMap":7},[184],{"type":18,"tag":113,"props":185,"children":186},{"class":115,"line":116},[187],{"type":18,"tag":113,"props":188,"children":189},{},[190],{"type":28,"value":179},{"type":18,"tag":76,"props":192,"children":194},{"id":193},"元字符",[195],{"type":28,"value":193},{"type":18,"tag":30,"props":197,"children":198},{},[199,201,206,208,213,215,221,222,228],{"type":28,"value":200},"除了前面介绍的元字符",{"type":18,"tag":51,"props":202,"children":204},{"className":203},[],[205],{"type":28,"value":91},{"type":28,"value":207},"和",{"type":18,"tag":51,"props":209,"children":211},{"className":210},[],[212],{"type":28,"value":99},{"type":28,"value":214},"外，正则表达式里还有更多的元字符，例如下面的正则表达式中就应用了元字符",{"type":18,"tag":51,"props":216,"children":218},{"className":217},[],[219],{"type":28,"value":220},"\\b",{"type":28,"value":207},{"type":18,"tag":51,"props":223,"children":225},{"className":224},[],[226],{"type":28,"value":227},"\\w",{"type":28,"value":229},"。",{"type":18,"tag":103,"props":231,"children":233},{"code":232,"language":106,"meta":7,"className":107,"style":7},"\\bmr\\w*\\b # 单词边界 + mr + 任意字符 + 单词边界\n",[234],{"type":18,"tag":51,"props":235,"children":236},{"__ignoreMap":7},[237],{"type":18,"tag":113,"props":238,"children":239},{"class":115,"line":116},[240],{"type":18,"tag":113,"props":241,"children":242},{},[243],{"type":28,"value":232},{"type":18,"tag":30,"props":245,"children":246},{},[247,249,255,257,262,264,269,271,277,279,284,286,292,294,300,301,307,309,315],{"type":28,"value":248},"上面的正则表达式用于匹配以字母",{"type":18,"tag":51,"props":250,"children":252},{"className":251},[],[253],{"type":28,"value":254},"mr",{"type":28,"value":256},"开头的单词，先从某个单词开始处",{"type":18,"tag":51,"props":258,"children":260},{"className":259},[],[261],{"type":28,"value":220},{"type":28,"value":263},"，然后匹配字母",{"type":18,"tag":51,"props":265,"children":267},{"className":266},[],[268],{"type":28,"value":254},{"type":28,"value":270},"，接着是任意数量的字母或数字 ",{"type":18,"tag":51,"props":272,"children":274},{"className":273},[],[275],{"type":28,"value":276},"\\w*",{"type":28,"value":278},"，最后单词结束处",{"type":18,"tag":51,"props":280,"children":282},{"className":281},[],[283],{"type":28,"value":220},{"type":28,"value":285},"。该表达式可以匹配",{"type":18,"tag":51,"props":287,"children":289},{"className":288},[],[290],{"type":28,"value":291},"mrsoft",{"type":28,"value":293},"、",{"type":18,"tag":51,"props":295,"children":297},{"className":296},[],[298],{"type":28,"value":299},"\\nmr",{"type":28,"value":207},{"type":18,"tag":51,"props":302,"children":304},{"className":303},[],[305],{"type":28,"value":306},"mr123456",{"type":28,"value":308},"等，但不能与",{"type":18,"tag":51,"props":310,"children":312},{"className":311},[],[313],{"type":28,"value":314},"amr",{"type":28,"value":316},"匹配。更多常用元字符如表所示。",{"type":18,"tag":30,"props":318,"children":319},{},[320],{"type":18,"tag":36,"props":321,"children":322},{},[323],{"type":28,"value":324},"常用元字符表",{"type":18,"tag":326,"props":327,"children":328},"table",{},[329,363],{"type":18,"tag":330,"props":331,"children":332},"thead",{},[333],{"type":18,"tag":334,"props":335,"children":336},"tr",{},[337,347,355],{"type":18,"tag":338,"props":339,"children":341},"th",{"align":340},"center",[342],{"type":18,"tag":36,"props":343,"children":344},{},[345],{"type":28,"value":346},"代    码",{"type":18,"tag":338,"props":348,"children":349},{"align":340},[350],{"type":18,"tag":36,"props":351,"children":352},{},[353],{"type":28,"value":354},"说    明",{"type":18,"tag":338,"props":356,"children":357},{"align":340},[358],{"type":18,"tag":36,"props":359,"children":360},{},[361],{"type":28,"value":362},"举    例",{"type":18,"tag":364,"props":365,"children":366},"tbody",{},[367,390,411,433,455,477,498],{"type":18,"tag":334,"props":368,"children":369},{},[370,380,385],{"type":18,"tag":371,"props":372,"children":373},"td",{"align":340},[374],{"type":18,"tag":51,"props":375,"children":377},{"className":376},[],[378],{"type":28,"value":379},".",{"type":18,"tag":371,"props":381,"children":382},{"align":340},[383],{"type":28,"value":384},"匹配除换行符以外的任意字符",{"type":18,"tag":371,"props":386,"children":387},{"align":340},[388],{"type":28,"value":389},".可以匹配“mr\\nM\\tR”中的m、r、M、\\t、R",{"type":18,"tag":334,"props":391,"children":392},{},[393,401,406],{"type":18,"tag":371,"props":394,"children":395},{"align":340},[396],{"type":18,"tag":51,"props":397,"children":399},{"className":398},[],[400],{"type":28,"value":227},{"type":18,"tag":371,"props":402,"children":403},{"align":340},[404],{"type":28,"value":405},"匹配字母、数字、下划线或汉字",{"type":18,"tag":371,"props":407,"children":408},{"align":340},[409],{"type":28,"value":410},"\\w可以匹配“m中7r\\n”中的“m、中、7、r”，但不能匹配\\n",{"type":18,"tag":334,"props":412,"children":413},{},[414,423,428],{"type":18,"tag":371,"props":415,"children":416},{"align":340},[417],{"type":18,"tag":51,"props":418,"children":420},{"className":419},[],[421],{"type":28,"value":422},"\\W",{"type":18,"tag":371,"props":424,"children":425},{"align":340},[426],{"type":28,"value":427},"匹配除字母、数字、下划线或汉字以外的字符",{"type":18,"tag":371,"props":429,"children":430},{"align":340},[431],{"type":28,"value":432},"\\W可以匹配“m中7r\\n”中的\\n，但不能匹配“m、中、7、r”",{"type":18,"tag":334,"props":434,"children":435},{},[436,445,450],{"type":18,"tag":371,"props":437,"children":438},{"align":340},[439],{"type":18,"tag":51,"props":440,"children":442},{"className":441},[],[443],{"type":28,"value":444},"\\s",{"type":18,"tag":371,"props":446,"children":447},{"align":340},[448],{"type":28,"value":449},"匹配单个的空白符（包括Tab键和换行符）",{"type":18,"tag":371,"props":451,"children":452},{"align":340},[453],{"type":28,"value":454},"\\s可以匹配“mr\\tMR”中的\\t",{"type":18,"tag":334,"props":456,"children":457},{},[458,467,472],{"type":18,"tag":371,"props":459,"children":460},{"align":340},[461],{"type":18,"tag":51,"props":462,"children":464},{"className":463},[],[465],{"type":28,"value":466},"\\S",{"type":18,"tag":371,"props":468,"children":469},{"align":340},[470],{"type":28,"value":471},"除单个空白符（包括Tab键和换行符）以外的所有字符",{"type":18,"tag":371,"props":473,"children":474},{"align":340},[475],{"type":28,"value":476},"\\S或以匹配“mr\\tMR”中的m、r、M、R",{"type":18,"tag":334,"props":478,"children":479},{},[480,488,493],{"type":18,"tag":371,"props":481,"children":482},{"align":340},[483],{"type":18,"tag":51,"props":484,"children":486},{"className":485},[],[487],{"type":28,"value":220},{"type":18,"tag":371,"props":489,"children":490},{"align":340},[491],{"type":28,"value":492},"匹配单词的开始或结束，单词的分界符通常是空格，标点符号或者换行",{"type":18,"tag":371,"props":494,"children":495},{"align":340},[496],{"type":28,"value":497},"在“I like mr or am”字符串中，\\bm与mr中的m相匹配，但与am中的m不匹配",{"type":18,"tag":334,"props":499,"children":500},{},[501,510,515],{"type":18,"tag":371,"props":502,"children":503},{"align":340},[504],{"type":18,"tag":51,"props":505,"children":507},{"className":506},[],[508],{"type":28,"value":509},"\\d",{"type":18,"tag":371,"props":511,"children":512},{"align":340},[513],{"type":28,"value":514},"匹配数字",{"type":18,"tag":371,"props":516,"children":517},{"align":340},[518],{"type":28,"value":519},"\\d可以与“m7ri”中的字符7匹配",{"type":18,"tag":76,"props":521,"children":523},{"id":522},"限定符",[524],{"type":28,"value":522},{"type":18,"tag":30,"props":526,"children":527},{},[528,530,535],{"type":28,"value":529},"在上面例子中，使用",{"type":18,"tag":51,"props":531,"children":533},{"className":532},[],[534],{"type":28,"value":276},{"type":28,"value":536},"匹配任意数量的字母或数字。如果想匹配特定数量的数字，该如何表示呢？正则表达式为我们提供了限定符（指定数量的字符）来实现该功能。如匹配8位QQ号可用如下表达式：",{"type":18,"tag":103,"props":538,"children":540},{"code":539,"language":106,"meta":7,"className":107,"style":7},"^\\d{8}$\n",[541],{"type":18,"tag":51,"props":542,"children":543},{"__ignoreMap":7},[544],{"type":18,"tag":113,"props":545,"children":546},{"class":115,"line":116},[547],{"type":18,"tag":113,"props":548,"children":549},{},[550],{"type":28,"value":539},{"type":18,"tag":30,"props":552,"children":553},{},[554],{"type":28,"value":555},"常用的限定符如表所示。",{"type":18,"tag":30,"props":557,"children":558},{},[559],{"type":18,"tag":36,"props":560,"children":561},{},[562],{"type":28,"value":563},"常用限定符表",{"type":18,"tag":326,"props":565,"children":566},{},[567,595],{"type":18,"tag":330,"props":568,"children":569},{},[570],{"type":18,"tag":334,"props":571,"children":572},{},[573,581,588],{"type":18,"tag":338,"props":574,"children":575},{"align":340},[576],{"type":18,"tag":36,"props":577,"children":578},{},[579],{"type":28,"value":580},"限  定  符",{"type":18,"tag":338,"props":582,"children":583},{"align":340},[584],{"type":18,"tag":36,"props":585,"children":586},{},[587],{"type":28,"value":354},{"type":18,"tag":338,"props":589,"children":590},{"align":340},[591],{"type":18,"tag":36,"props":592,"children":593},{},[594],{"type":28,"value":362},{"type":18,"tag":364,"props":596,"children":597},{},[598,620,642,664,686,708],{"type":18,"tag":334,"props":599,"children":600},{},[601,610,615],{"type":18,"tag":371,"props":602,"children":603},{"align":340},[604],{"type":18,"tag":51,"props":605,"children":607},{"className":606},[],[608],{"type":28,"value":609},"?",{"type":18,"tag":371,"props":611,"children":612},{"align":340},[613],{"type":28,"value":614},"匹配前面的字符零次或一次",{"type":18,"tag":371,"props":616,"children":617},{"align":340},[618],{"type":28,"value":619},"colou?r，该表达式可以匹配colour和color",{"type":18,"tag":334,"props":621,"children":622},{},[623,632,637],{"type":18,"tag":371,"props":624,"children":625},{"align":340},[626],{"type":18,"tag":51,"props":627,"children":629},{"className":628},[],[630],{"type":28,"value":631},"+",{"type":18,"tag":371,"props":633,"children":634},{"align":340},[635],{"type":28,"value":636},"匹配前面的字符一次或多次",{"type":18,"tag":371,"props":638,"children":639},{"align":340},[640],{"type":28,"value":641},"go+gle，该表达式可以匹配的范围从gogle到goo…gle",{"type":18,"tag":334,"props":643,"children":644},{},[645,654,659],{"type":18,"tag":371,"props":646,"children":647},{"align":340},[648],{"type":18,"tag":51,"props":649,"children":651},{"className":650},[],[652],{"type":28,"value":653},"*",{"type":18,"tag":371,"props":655,"children":656},{"align":340},[657],{"type":28,"value":658},"匹配前面的字符零次或多次",{"type":18,"tag":371,"props":660,"children":661},{"align":340},[662],{"type":28,"value":663},"go*gle，该表达式可以匹配的范围从ggle到goo…gle",{"type":18,"tag":334,"props":665,"children":666},{},[667,676,681],{"type":18,"tag":371,"props":668,"children":669},{"align":340},[670],{"type":18,"tag":51,"props":671,"children":673},{"className":672},[],[674],{"type":28,"value":675},"{n}",{"type":18,"tag":371,"props":677,"children":678},{"align":340},[679],{"type":28,"value":680},"匹配前面的字符n次",{"type":18,"tag":371,"props":682,"children":683},{"align":340},[684],{"type":28,"value":685},"go{2}gle，该表达式只匹配google",{"type":18,"tag":334,"props":687,"children":688},{},[689,698,703],{"type":18,"tag":371,"props":690,"children":691},{"align":340},[692],{"type":18,"tag":51,"props":693,"children":695},{"className":694},[],[696],{"type":28,"value":697},"{n,}",{"type":18,"tag":371,"props":699,"children":700},{"align":340},[701],{"type":28,"value":702},"匹配前面的字符最少n次",{"type":18,"tag":371,"props":704,"children":705},{"align":340},[706],{"type":28,"value":707},"go{2,}gle，该表达式可以匹配的范围从google到goo…gle",{"type":18,"tag":334,"props":709,"children":710},{},[711,720,725],{"type":18,"tag":371,"props":712,"children":713},{"align":340},[714],{"type":18,"tag":51,"props":715,"children":717},{"className":716},[],[718],{"type":28,"value":719},"{n,m}",{"type":18,"tag":371,"props":721,"children":722},{"align":340},[723],{"type":28,"value":724},"匹配前面的字符最少n次，最多m次",{"type":18,"tag":371,"props":726,"children":727},{"align":340},[728],{"type":28,"value":729},"employe{0,2}，该表达式可以匹配employ、employe和employee 3种情况",{"type":18,"tag":76,"props":731,"children":733},{"id":732},"字符类",[734],{"type":28,"value":732},{"type":18,"tag":30,"props":736,"children":737},{},[738],{"type":28,"value":739},"正则表达式查找数字和字母是很简单的，因为已经有了对应这些字符集合的元字符（如“\\d”“\\w”），但是如果要匹配没有预定义元字符的字符集合(比如元音字母 a, e, i, o, u )，应该怎么办？",{"type":18,"tag":30,"props":741,"children":742},{},[743,745,750,752,758,760,766,768,774,776,782,784,789],{"type":28,"value":744},"很简单，只需要",{"type":18,"tag":36,"props":746,"children":747},{},[748],{"type":28,"value":749},"在方括号里列出它们",{"type":28,"value":751},"就行了，像",{"type":18,"tag":51,"props":753,"children":755},{"className":754},[],[756],{"type":28,"value":757},"[aeiou]",{"type":28,"value":759},"可以匹配任何一个英文元音字母，",{"type":18,"tag":51,"props":761,"children":763},{"className":762},[],[764],{"type":28,"value":765},"[.?!]",{"type":28,"value":767}," 匹配标点符号(“.”“?”或“!”)。也可以轻松地指定一个字符范围，像",{"type":18,"tag":51,"props":769,"children":771},{"className":770},[],[772],{"type":28,"value":773},"[0-9]",{"type":28,"value":775},"代表的含义与“\\d”就是完全一致的，即表示一位数字；同理，",{"type":18,"tag":51,"props":777,"children":779},{"className":778},[],[780],{"type":28,"value":781},"[a-z0-9A-Z_]",{"type":28,"value":783},"完全等同于",{"type":18,"tag":51,"props":785,"children":787},{"className":786},[],[788],{"type":28,"value":227},{"type":28,"value":790},"（如果只考虑英文的话）。",{"type":18,"tag":792,"props":793,"children":794},"blockquote",{},[795],{"type":18,"tag":30,"props":796,"children":797},{},[798,800,806,808,814],{"type":28,"value":799},"说明：要想匹配给定字符串中任意一个汉字，可以使用",{"type":18,"tag":51,"props":801,"children":803},{"className":802},[],[804],{"type":28,"value":805},"[\\u4e00-\\u9fa5]",{"type":28,"value":807},"；如果要匹配连续多个汉字，可以使用",{"type":18,"tag":51,"props":809,"children":811},{"className":810},[],[812],{"type":28,"value":813},"[\\u4e00-\\u9fa5]+",{"type":28,"value":229},{"type":18,"tag":76,"props":816,"children":818},{"id":817},"排除字符",[819],{"type":28,"value":817},{"type":18,"tag":30,"props":821,"children":822},{},[823,825,830,832,844],{"type":28,"value":824},"上述列出的是匹配符合指定字符集合的字符串。现在反过来，匹配不符合指定字符集合的字符串。正则表达式提供了",{"type":18,"tag":51,"props":826,"children":828},{"className":827},[],[829],{"type":28,"value":91},{"type":28,"value":831},"字符。这个元字符在上述文章中出现过，表示行的开始。而",{"type":18,"tag":36,"props":833,"children":834},{},[835,837,842],{"type":28,"value":836},"这里将",{"type":18,"tag":51,"props":838,"children":840},{"className":839},[],[841],{"type":28,"value":91},{"type":28,"value":843},"放到方括号中，表示排除",{"type":28,"value":845},"的意思。例如：",{"type":18,"tag":103,"props":847,"children":849},{"code":848,"language":106,"meta":7,"className":107,"style":7},"[^a-zA-Z]\n",[850],{"type":18,"tag":51,"props":851,"children":852},{"__ignoreMap":7},[853],{"type":18,"tag":113,"props":854,"children":855},{"class":115,"line":116},[856],{"type":18,"tag":113,"props":857,"children":858},{},[859],{"type":28,"value":848},{"type":18,"tag":30,"props":861,"children":862},{},[863],{"type":28,"value":864},"该表达式用于匹配一个不是字母的字符。",{"type":18,"tag":76,"props":866,"children":868},{"id":867},"选择字符",[869],{"type":28,"value":867},{"type":18,"tag":30,"props":871,"children":872},{},[873],{"type":28,"value":874},"试想一下，如何匹配身份证号码？首先需要了解一下身份证号码的规则。身份证号码长度为15位或者18位。如果为15位时，则全为数字；如果为18位时，前17位为数字，最后一位是校验位，可能为数字或字符X。",{"type":18,"tag":30,"props":876,"children":877},{},[878,880,886],{"type":28,"value":879},"在上面的描述中，包含着条件选择的逻辑，这就需要使用选择字符 ",{"type":18,"tag":51,"props":881,"children":883},{"className":882},[],[884],{"type":28,"value":885},"|",{"type":28,"value":887}," 来实现。该字符可以理解为“或”，匹配身份证的表达式可以写成如下方式：",{"type":18,"tag":103,"props":889,"children":891},{"code":890,"language":106,"meta":7,"className":107,"style":7},"(^\\d{15}$)|(^\\d{18}$)|(^\\d{17})(\\d|X|x)$\n",[892],{"type":18,"tag":51,"props":893,"children":894},{"__ignoreMap":7},[895],{"type":18,"tag":113,"props":896,"children":897},{"class":115,"line":116},[898],{"type":18,"tag":113,"props":899,"children":900},{},[901],{"type":28,"value":890},{"type":18,"tag":30,"props":903,"children":904},{},[905],{"type":28,"value":906},"该表达式的意思是以匹配15位数字，或者18位数字，或者17位数字和最后一位。最后一位可以是数字，也可以是X或者x。",{"type":18,"tag":76,"props":908,"children":910},{"id":909},"转义字符",[911],{"type":28,"value":909},{"type":18,"tag":30,"props":913,"children":914},{},[915,917,923],{"type":28,"value":916},"正则表达式中的转义字符 ",{"type":18,"tag":51,"props":918,"children":920},{"className":919},[],[921],{"type":28,"value":922},"\\",{"type":28,"value":924}," 和Python中的大同小异，都是将特殊字符（如“.”、“?”、“\\”等）变为普通的字符。举一个IP地址的实例，用正则表达式匹配诸如“127.0.0.1”格式的IP地址。如果直接使用点字符，格式为：",{"type":18,"tag":103,"props":926,"children":928},{"code":927,"language":106,"meta":7,"className":107,"style":7},"[1-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3} # 这个是错误示例\n",[929],{"type":18,"tag":51,"props":930,"children":931},{"__ignoreMap":7},[932],{"type":18,"tag":113,"props":933,"children":934},{"class":115,"line":116},[935],{"type":18,"tag":113,"props":936,"children":937},{},[938],{"type":28,"value":927},{"type":18,"tag":30,"props":940,"children":941},{},[942,944,949,951,956,958,963],{"type":28,"value":943},"这显然不对，因为",{"type":18,"tag":51,"props":945,"children":947},{"className":946},[],[948],{"type":28,"value":379},{"type":28,"value":950},"可以匹配一个任意字符。这时，不仅是127.0.0.1这样的IP，连127101011这样的字符串也会被匹配出来。所以在使用",{"type":18,"tag":51,"props":952,"children":954},{"className":953},[],[955],{"type":28,"value":379},{"type":28,"value":957},"时，需要使用转义字符",{"type":18,"tag":51,"props":959,"children":961},{"className":960},[],[962],{"type":28,"value":922},{"type":28,"value":964},"。修改后上面的正则表达式格式为：",{"type":18,"tag":103,"props":966,"children":968},{"code":967,"language":106,"meta":7,"className":107,"style":7},"[1-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\n",[969],{"type":18,"tag":51,"props":970,"children":971},{"__ignoreMap":7},[972],{"type":18,"tag":113,"props":973,"children":974},{"class":115,"line":116},[975],{"type":18,"tag":113,"props":976,"children":977},{},[978],{"type":28,"value":967},{"type":18,"tag":792,"props":980,"children":981},{},[982],{"type":18,"tag":30,"props":983,"children":984},{},[985],{"type":28,"value":986},"说明：括号在正则表达式中也算是一个元字符。",{"type":18,"tag":76,"props":988,"children":990},{"id":989},"分组",[991],{"type":28,"value":989},{"type":18,"tag":30,"props":993,"children":994},{},[995,997,1002,1004,1009,1011,1016,1017,1022,1023,1028],{"type":28,"value":996},"通过选择字符的例子，已经对",{"type":18,"tag":36,"props":998,"children":999},{},[1000],{"type":28,"value":1001},"小括号的作用",{"type":28,"value":1003},"有了一定的了解。小括号字符的第一个作用就是",{"type":18,"tag":36,"props":1005,"children":1006},{},[1007],{"type":28,"value":1008},"可以改变限定符的作用范围",{"type":28,"value":1010},"，如",{"type":18,"tag":51,"props":1012,"children":1014},{"className":1013},[],[1015],{"type":28,"value":885},{"type":28,"value":293},{"type":18,"tag":51,"props":1018,"children":1020},{"className":1019},[],[1021],{"type":28,"value":653},{"type":28,"value":293},{"type":18,"tag":51,"props":1024,"children":1026},{"className":1025},[],[1027],{"type":28,"value":91},{"type":28,"value":1029},"等。例如下面的表达式中包含小括号。",{"type":18,"tag":103,"props":1031,"children":1033},{"code":1032,"language":106,"meta":7,"className":107,"style":7},"(six|four)th\n",[1034],{"type":18,"tag":51,"props":1035,"children":1036},{"__ignoreMap":7},[1037],{"type":18,"tag":113,"props":1038,"children":1039},{"class":115,"line":116},[1040],{"type":18,"tag":113,"props":1041,"children":1042},{},[1043],{"type":28,"value":1032},{"type":18,"tag":30,"props":1045,"children":1046},{},[1047],{"type":28,"value":1048},"这个表达式的意思是匹配单词sixth或fourth，如果不使用小括号，那么就变成了匹配单词six和fourth了。",{"type":18,"tag":30,"props":1050,"children":1051},{},[1052,1054,1058,1060,1066,1068,1074],{"type":28,"value":1053},"小括号的第二个作用是",{"type":18,"tag":36,"props":1055,"children":1056},{},[1057],{"type":28,"value":989},{"type":28,"value":1059},"，也就是子表达式。如",{"type":18,"tag":51,"props":1061,"children":1063},{"className":1062},[],[1064],{"type":28,"value":1065},"(\\.[0-9]{1,3}){3}",{"type":28,"value":1067},"，就是对分组",{"type":18,"tag":51,"props":1069,"children":1071},{"className":1070},[],[1072],{"type":28,"value":1073},"(\\.[0-9]{1,3})",{"type":28,"value":1075},"进行重复操作。",{"type":18,"tag":76,"props":1077,"children":1079},{"id":1078},"在python中使用正则表达式语法",[1080],{"type":28,"value":1081},"在Python中使用正则表达式语法",{"type":18,"tag":30,"props":1083,"children":1084},{},[1085],{"type":28,"value":1086},"在Python中使用正则表达式时，是将其作为模式字符串使用的。例如，将匹配不是字母的一个字符的正则表达式表示为模式字符串，可以使用下面的代码：",{"type":18,"tag":103,"props":1088,"children":1090},{"code":1089,"language":106,"meta":7,"className":107,"style":7},"'[^a-zA-Z]'\n",[1091],{"type":18,"tag":51,"props":1092,"children":1093},{"__ignoreMap":7},[1094],{"type":18,"tag":113,"props":1095,"children":1096},{"class":115,"line":116},[1097],{"type":18,"tag":113,"props":1098,"children":1099},{},[1100],{"type":28,"value":1089},{"type":18,"tag":30,"props":1102,"children":1103},{},[1104],{"type":28,"value":1105},"而如果将匹配以字母m开头的单词的正则表达式转换为模式字符串，则不能直接在其两侧添加引号定界符，例如，下面的代码是不正确的。",{"type":18,"tag":103,"props":1107,"children":1109},{"code":1108,"language":106,"meta":7,"className":107,"style":7},"'\\bm\\w*\\b'  # 错误示范\n",[1110],{"type":18,"tag":51,"props":1111,"children":1112},{"__ignoreMap":7},[1113],{"type":18,"tag":113,"props":1114,"children":1115},{"class":115,"line":116},[1116],{"type":18,"tag":113,"props":1117,"children":1118},{},[1119],{"type":28,"value":1108},{"type":18,"tag":30,"props":1121,"children":1122},{},[1123],{"type":28,"value":1124},"而是需要将其中的“\\”进行转义，转换后的结果为：",{"type":18,"tag":103,"props":1126,"children":1128},{"code":1127,"language":106,"meta":7,"className":107,"style":7},"'\\\\bm\\\\w*\\\\b'\n",[1129],{"type":18,"tag":51,"props":1130,"children":1131},{"__ignoreMap":7},[1132],{"type":18,"tag":113,"props":1133,"children":1134},{"class":115,"line":116},[1135],{"type":18,"tag":113,"props":1136,"children":1137},{},[1138],{"type":28,"value":1127},{"type":18,"tag":30,"props":1140,"children":1141},{},[1142],{"type":28,"value":1143},"由于模式字符串中可能包括大量的特殊字符和反斜杠，所以需要写为原生字符串，即在模式字符串前加r或R。例如，上面的模式字符串采用原生字符串表示为：",{"type":18,"tag":103,"props":1145,"children":1147},{"code":1146,"language":106,"meta":7,"className":107,"style":7},"r'\\bm\\w*\\b'\n",[1148],{"type":18,"tag":51,"props":1149,"children":1150},{"__ignoreMap":7},[1151],{"type":18,"tag":113,"props":1152,"children":1153},{"class":115,"line":116},[1154],{"type":18,"tag":113,"props":1155,"children":1156},{},[1157],{"type":28,"value":1146},{"type":18,"tag":792,"props":1159,"children":1160},{},[1161],{"type":18,"tag":30,"props":1162,"children":1163},{},[1164,1166,1172],{"type":28,"value":1165},"说明：在编写模式字符串时，并不是所有的反斜杠都需要进行转换，例如，前面编写的正则表达式",{"type":18,"tag":51,"props":1167,"children":1169},{"className":1168},[],[1170],{"type":28,"value":1171},"^\\d{8}$",{"type":28,"value":1173},"中的反斜杠就不需要转义，因为其中的\\d并没有特殊意义。不过，为了编写方便，这里的正则表达式都采用原生字符串表示。",{"type":18,"tag":23,"props":1175,"children":1177},{"id":1176},"匹配字符串",[1178],{"type":28,"value":1176},{"type":18,"tag":30,"props":1180,"children":1181},{},[1182,1184,1190,1192,1197,1199,1205,1206,1212,1213,1219,1221,1227],{"type":28,"value":1183},"Python提供了",{"type":18,"tag":51,"props":1185,"children":1187},{"className":1186},[],[1188],{"type":28,"value":1189},"re",{"type":28,"value":1191},"模块，用于实现正则表达式的操作。在实现时，可以使用",{"type":18,"tag":51,"props":1193,"children":1195},{"className":1194},[],[1196],{"type":28,"value":1189},{"type":28,"value":1198},"模块提供的方法（如",{"type":18,"tag":51,"props":1200,"children":1202},{"className":1201},[],[1203],{"type":28,"value":1204},"search()",{"type":28,"value":293},{"type":18,"tag":51,"props":1207,"children":1209},{"className":1208},[],[1210],{"type":28,"value":1211},"match()",{"type":28,"value":293},{"type":18,"tag":51,"props":1214,"children":1216},{"className":1215},[],[1217],{"type":28,"value":1218},"findall()",{"type":28,"value":1220},"等）进行字符串处理，也可以先使用re模块的",{"type":18,"tag":51,"props":1222,"children":1224},{"className":1223},[],[1225],{"type":28,"value":1226},"compile()",{"type":28,"value":1228},"方法将模式字符串转换为正则表达式对象，然后再使用该正则表达式对象的相关方法来操作字符串。",{"type":18,"tag":30,"props":1230,"children":1231},{},[1232,1237],{"type":18,"tag":51,"props":1233,"children":1235},{"className":1234},[],[1236],{"type":28,"value":1189},{"type":28,"value":1238},"模块在使用时，需要先应用import语句引入，具体代码如下：",{"type":18,"tag":103,"props":1240,"children":1242},{"code":1241,"language":106,"meta":7,"className":107,"style":7},"import re\n",[1243],{"type":18,"tag":51,"props":1244,"children":1245},{"__ignoreMap":7},[1246],{"type":18,"tag":113,"props":1247,"children":1248},{"class":115,"line":116},[1249],{"type":18,"tag":113,"props":1250,"children":1251},{},[1252],{"type":28,"value":1241},{"type":18,"tag":30,"props":1254,"children":1255},{},[1256],{"type":28,"value":1257},"如果在使用re模块时，没有将其引入，将抛出如图所示的异常。",{"type":18,"tag":30,"props":1259,"children":1260},{},[1261],{"type":18,"tag":1262,"props":1263,"children":1266},"img",{"alt":1264,"src":1265},"Pasted image 20250720092257","\u002Fimages\u002Fblog\u002Fpython-basics\u002FPasted%20image%2020250720092257.png",[],{"type":18,"tag":30,"props":1268,"children":1269},{},[1270,1272,1277,1278,1283,1284,1289],{"type":28,"value":1271},"匹配字符串可以使用re模块提供的",{"type":18,"tag":51,"props":1273,"children":1275},{"className":1274},[],[1276],{"type":28,"value":1211},{"type":28,"value":293},{"type":18,"tag":51,"props":1279,"children":1281},{"className":1280},[],[1282],{"type":28,"value":1204},{"type":28,"value":207},{"type":18,"tag":51,"props":1285,"children":1287},{"className":1286},[],[1288],{"type":28,"value":1218},{"type":28,"value":1290},"等方法。",{"type":18,"tag":76,"props":1292,"children":1294},{"id":1293},"使用match方法进行匹配",[1295],{"type":28,"value":1296},"使用match()方法进行匹配",{"type":18,"tag":30,"props":1298,"children":1299},{},[1300,1305],{"type":18,"tag":51,"props":1301,"children":1303},{"className":1302},[],[1304],{"type":28,"value":1211},{"type":28,"value":1306},"方法用于从字符串的开始处进行匹配，如果在起始位置匹配成功，则返回Match对象，否则返回None。其语法格式如下：",{"type":18,"tag":103,"props":1308,"children":1310},{"code":1309,"language":106,"meta":7,"className":107,"style":7},"re.match(pattern, string, [flags])\n",[1311],{"type":18,"tag":51,"props":1312,"children":1313},{"__ignoreMap":7},[1314],{"type":18,"tag":113,"props":1315,"children":1316},{"class":115,"line":116},[1317],{"type":18,"tag":113,"props":1318,"children":1319},{},[1320],{"type":28,"value":1309},{"type":18,"tag":30,"props":1322,"children":1323},{},[1324],{"type":28,"value":1325},"参数说明：",{"type":18,"tag":1327,"props":1328,"children":1329},"ul",{},[1330,1342,1353],{"type":18,"tag":1331,"props":1332,"children":1333},"li",{},[1334,1340],{"type":18,"tag":51,"props":1335,"children":1337},{"className":1336},[],[1338],{"type":28,"value":1339},"pattern",{"type":28,"value":1341},"：表示模式字符串，由要匹配的正则表达式转换而来。",{"type":18,"tag":1331,"props":1343,"children":1344},{},[1345,1351],{"type":18,"tag":51,"props":1346,"children":1348},{"className":1347},[],[1349],{"type":28,"value":1350},"string",{"type":28,"value":1352},"：表示要匹配的字符串。",{"type":18,"tag":1331,"props":1354,"children":1355},{},[1356,1362],{"type":18,"tag":51,"props":1357,"children":1359},{"className":1358},[],[1360],{"type":28,"value":1361},"flags",{"type":28,"value":1363},"：可选参数，表示标志位，用于控制匹配方式，如是否区分字母大小写。常用的标志如下表所示。",{"type":18,"tag":326,"props":1365,"children":1366},{},[1367,1388],{"type":18,"tag":330,"props":1368,"children":1369},{},[1370],{"type":18,"tag":334,"props":1371,"children":1372},{},[1373,1381],{"type":18,"tag":338,"props":1374,"children":1375},{"align":340},[1376],{"type":18,"tag":36,"props":1377,"children":1378},{},[1379],{"type":28,"value":1380},"标    志",{"type":18,"tag":338,"props":1382,"children":1383},{"align":340},[1384],{"type":18,"tag":36,"props":1385,"children":1386},{},[1387],{"type":28,"value":354},{"type":18,"tag":364,"props":1389,"children":1390},{},[1391,1404,1417,1430,1443],{"type":18,"tag":334,"props":1392,"children":1393},{},[1394,1399],{"type":18,"tag":371,"props":1395,"children":1396},{"align":340},[1397],{"type":28,"value":1398},"A或ASCII",{"type":18,"tag":371,"props":1400,"children":1401},{"align":340},[1402],{"type":28,"value":1403},"对于\\w、\\W、\\b、\\B、\\d、\\D、\\s和\\S只进行ASCII匹配（仅适用于Python 3.x）",{"type":18,"tag":334,"props":1405,"children":1406},{},[1407,1412],{"type":18,"tag":371,"props":1408,"children":1409},{"align":340},[1410],{"type":28,"value":1411},"I或IGNORECASE",{"type":18,"tag":371,"props":1413,"children":1414},{"align":340},[1415],{"type":28,"value":1416},"执行不区分字母大小写的匹配",{"type":18,"tag":334,"props":1418,"children":1419},{},[1420,1425],{"type":18,"tag":371,"props":1421,"children":1422},{"align":340},[1423],{"type":28,"value":1424},"M或MULTILINE",{"type":18,"tag":371,"props":1426,"children":1427},{"align":340},[1428],{"type":28,"value":1429},"将^和$用于包括整个字符串的开始和结尾的每一行（默认情况下，仅适用于整个字符串的开始和结尾处）",{"type":18,"tag":334,"props":1431,"children":1432},{},[1433,1438],{"type":18,"tag":371,"props":1434,"children":1435},{"align":340},[1436],{"type":28,"value":1437},"S或DOTALL",{"type":18,"tag":371,"props":1439,"children":1440},{"align":340},[1441],{"type":28,"value":1442},"使用（.）字符匹配所有字符，包括换行符",{"type":18,"tag":334,"props":1444,"children":1445},{},[1446,1451],{"type":18,"tag":371,"props":1447,"children":1448},{"align":340},[1449],{"type":28,"value":1450},"X或VERBOSE",{"type":18,"tag":371,"props":1452,"children":1453},{"align":340},[1454],{"type":28,"value":1455},"忽略模式字符串中未转义的空格和注释",{"type":18,"tag":30,"props":1457,"children":1458},{},[1459],{"type":28,"value":1460},"例如，匹配字符串是否以“mr_”开头，不区分字母大小写，代码如下：",{"type":18,"tag":103,"props":1462,"children":1464},{"code":1463,"language":106,"meta":7,"className":107,"style":7},"import re\npattern = r'mr_\\w+'                        # 模式字符串\nstring = 'MR_SHOP mr_shop'                  # 要匹配的字符串\nmatch = re.match(pattern,string,re.I)      # 匹配字符串，不区分大小写\nprint(match)                                  # 输出匹配结果\nstring = '项目名称MR_SHOP mr_shop'\nmatch = re.match(pattern,string,re.I)      # 匹配字符串，不区分大小写\nprint(match)                                  # 输出匹配结果\n",[1465],{"type":18,"tag":51,"props":1466,"children":1467},{"__ignoreMap":7},[1468,1475,1484,1493,1502,1511,1520,1528],{"type":18,"tag":113,"props":1469,"children":1470},{"class":115,"line":116},[1471],{"type":18,"tag":113,"props":1472,"children":1473},{},[1474],{"type":28,"value":1241},{"type":18,"tag":113,"props":1476,"children":1478},{"class":115,"line":1477},2,[1479],{"type":18,"tag":113,"props":1480,"children":1481},{},[1482],{"type":28,"value":1483},"pattern = r'mr_\\w+'                        # 模式字符串\n",{"type":18,"tag":113,"props":1485,"children":1487},{"class":115,"line":1486},3,[1488],{"type":18,"tag":113,"props":1489,"children":1490},{},[1491],{"type":28,"value":1492},"string = 'MR_SHOP mr_shop'                  # 要匹配的字符串\n",{"type":18,"tag":113,"props":1494,"children":1496},{"class":115,"line":1495},4,[1497],{"type":18,"tag":113,"props":1498,"children":1499},{},[1500],{"type":28,"value":1501},"match = re.match(pattern,string,re.I)      # 匹配字符串，不区分大小写\n",{"type":18,"tag":113,"props":1503,"children":1505},{"class":115,"line":1504},5,[1506],{"type":18,"tag":113,"props":1507,"children":1508},{},[1509],{"type":28,"value":1510},"print(match)                                  # 输出匹配结果\n",{"type":18,"tag":113,"props":1512,"children":1514},{"class":115,"line":1513},6,[1515],{"type":18,"tag":113,"props":1516,"children":1517},{},[1518],{"type":28,"value":1519},"string = '项目名称MR_SHOP mr_shop'\n",{"type":18,"tag":113,"props":1521,"children":1523},{"class":115,"line":1522},7,[1524],{"type":18,"tag":113,"props":1525,"children":1526},{},[1527],{"type":28,"value":1501},{"type":18,"tag":113,"props":1529,"children":1531},{"class":115,"line":1530},8,[1532],{"type":18,"tag":113,"props":1533,"children":1534},{},[1535],{"type":28,"value":1510},{"type":18,"tag":30,"props":1537,"children":1538},{},[1539],{"type":28,"value":1540},"执行结果如下：",{"type":18,"tag":103,"props":1542,"children":1544},{"code":1543},"\u003C_sre.SRE_Match object; span=(0, 7), match='MR_SHOP'>\nNone\n",[1545],{"type":18,"tag":51,"props":1546,"children":1547},{"__ignoreMap":7},[1548],{"type":28,"value":1543},{"type":18,"tag":30,"props":1550,"children":1551},{},[1552],{"type":28,"value":1553},"从上面的执行结果中可以看出，字符串“MR_SHOP”以“mr_”开头，将返回一个Match对象，而字符串“项目名称MR_SHOP”没有以“mr_”开头，将返回“None”。这是因为match()方法从字符串的开始位置开始匹配，当第一个字母不符合条件时，则不再进行匹配，直接返回None。",{"type":18,"tag":30,"props":1555,"children":1556},{},[1557,1562,1564,1569,1571,1584,1586,1591,1592,1604,1606,1611,1612,1617],{"type":18,"tag":36,"props":1558,"children":1559},{},[1560],{"type":28,"value":1561},"Match对象中包含了匹配值的位置和匹配数据",{"type":28,"value":1563},"。其中，",{"type":18,"tag":36,"props":1565,"children":1566},{},[1567],{"type":28,"value":1568},"要获取匹配值的起始位置",{"type":28,"value":1570},"可以",{"type":18,"tag":36,"props":1572,"children":1573},{},[1574,1576,1582],{"type":28,"value":1575},"使用Match对象的",{"type":18,"tag":51,"props":1577,"children":1579},{"className":1578},[],[1580],{"type":28,"value":1581},"start()",{"type":28,"value":1583},"方法",{"type":28,"value":1585},"；要",{"type":18,"tag":36,"props":1587,"children":1588},{},[1589],{"type":28,"value":1590},"获取匹配值的结束位置",{"type":28,"value":1570},{"type":18,"tag":36,"props":1593,"children":1594},{},[1595,1597,1603],{"type":28,"value":1596},"使用",{"type":18,"tag":51,"props":1598,"children":1600},{"className":1599},[],[1601],{"type":28,"value":1602},"end()",{"type":28,"value":1583},{"type":28,"value":1605},"；通过",{"type":18,"tag":36,"props":1607,"children":1608},{},[1609],{"type":28,"value":1610},"span()方法",{"type":28,"value":1570},{"type":18,"tag":36,"props":1613,"children":1614},{},[1615],{"type":28,"value":1616},"返回匹配位置的元组",{"type":28,"value":1618},"；通过string属性可以获取要匹配的字符串。例如下面的代码：",{"type":18,"tag":103,"props":1620,"children":1622},{"code":1621,"language":106,"meta":7,"className":107,"style":7},"import re\npattern = r'mr_\\w+'                               # 模式字符串\nstring = 'MR_SHOP mr_shop'                      # 要匹配的字符串\nmatch = re.match(pattern,string,re.I)          # 匹配字符串，不区分大小写\nprint('匹配值的起始位置：',match.start())\nprint('匹配值的结束位置：',match.end())\nprint('匹配位置的元组：',match.span())\nprint('要匹配的字符串：',match.string)\nprint('匹配数据：',match.group())\n",[1623],{"type":18,"tag":51,"props":1624,"children":1625},{"__ignoreMap":7},[1626,1633,1641,1649,1657,1665,1673,1681,1689],{"type":18,"tag":113,"props":1627,"children":1628},{"class":115,"line":116},[1629],{"type":18,"tag":113,"props":1630,"children":1631},{},[1632],{"type":28,"value":1241},{"type":18,"tag":113,"props":1634,"children":1635},{"class":115,"line":1477},[1636],{"type":18,"tag":113,"props":1637,"children":1638},{},[1639],{"type":28,"value":1640},"pattern = r'mr_\\w+'                               # 模式字符串\n",{"type":18,"tag":113,"props":1642,"children":1643},{"class":115,"line":1486},[1644],{"type":18,"tag":113,"props":1645,"children":1646},{},[1647],{"type":28,"value":1648},"string = 'MR_SHOP mr_shop'                      # 要匹配的字符串\n",{"type":18,"tag":113,"props":1650,"children":1651},{"class":115,"line":1495},[1652],{"type":18,"tag":113,"props":1653,"children":1654},{},[1655],{"type":28,"value":1656},"match = re.match(pattern,string,re.I)          # 匹配字符串，不区分大小写\n",{"type":18,"tag":113,"props":1658,"children":1659},{"class":115,"line":1504},[1660],{"type":18,"tag":113,"props":1661,"children":1662},{},[1663],{"type":28,"value":1664},"print('匹配值的起始位置：',match.start())\n",{"type":18,"tag":113,"props":1666,"children":1667},{"class":115,"line":1513},[1668],{"type":18,"tag":113,"props":1669,"children":1670},{},[1671],{"type":28,"value":1672},"print('匹配值的结束位置：',match.end())\n",{"type":18,"tag":113,"props":1674,"children":1675},{"class":115,"line":1522},[1676],{"type":18,"tag":113,"props":1677,"children":1678},{},[1679],{"type":28,"value":1680},"print('匹配位置的元组：',match.span())\n",{"type":18,"tag":113,"props":1682,"children":1683},{"class":115,"line":1530},[1684],{"type":18,"tag":113,"props":1685,"children":1686},{},[1687],{"type":28,"value":1688},"print('要匹配的字符串：',match.string)\n",{"type":18,"tag":113,"props":1690,"children":1692},{"class":115,"line":1691},9,[1693],{"type":18,"tag":113,"props":1694,"children":1695},{},[1696],{"type":28,"value":1697},"print('匹配数据：',match.group())\n",{"type":18,"tag":30,"props":1699,"children":1700},{},[1701],{"type":28,"value":1540},{"type":18,"tag":103,"props":1703,"children":1705},{"code":1704},"匹配值的起始位置： 0\n匹配值的结束位置： 7\n匹配位置的元组： (0, 7)\n要匹配字符串： MR_SHOP mr_shop\n匹配数据： MR_SHOP\n",[1706],{"type":18,"tag":51,"props":1707,"children":1708},{"__ignoreMap":7},[1709],{"type":28,"value":1704},{"type":18,"tag":76,"props":1711,"children":1713},{"id":1712},"使用search方法进行匹配",[1714],{"type":28,"value":1715},"使用search()方法进行匹配",{"type":18,"tag":30,"props":1717,"children":1718},{},[1719,1724,1726,1731,1733,1738],{"type":18,"tag":51,"props":1720,"children":1722},{"className":1721},[],[1723],{"type":28,"value":1204},{"type":28,"value":1725},"方法用于",{"type":18,"tag":36,"props":1727,"children":1728},{},[1729],{"type":28,"value":1730},"在整个字符串中搜索第一个匹配的值",{"type":28,"value":1732},"，如果匹配成功，则返回Match对象，否则返回None。",{"type":18,"tag":51,"props":1734,"children":1736},{"className":1735},[],[1737],{"type":28,"value":1204},{"type":28,"value":1739},"方法的语法格式如下：",{"type":18,"tag":103,"props":1741,"children":1743},{"code":1742,"language":106,"meta":7,"className":107,"style":7},"re.search(pattern, string, [flags])\n",[1744],{"type":18,"tag":51,"props":1745,"children":1746},{"__ignoreMap":7},[1747],{"type":18,"tag":113,"props":1748,"children":1749},{"class":115,"line":116},[1750],{"type":18,"tag":113,"props":1751,"children":1752},{},[1753],{"type":28,"value":1742},{"type":18,"tag":30,"props":1755,"children":1756},{},[1757],{"type":28,"value":1325},{"type":18,"tag":1327,"props":1759,"children":1760},{},[1761,1770,1779],{"type":18,"tag":1331,"props":1762,"children":1763},{},[1764,1769],{"type":18,"tag":51,"props":1765,"children":1767},{"className":1766},[],[1768],{"type":28,"value":1339},{"type":28,"value":1341},{"type":18,"tag":1331,"props":1771,"children":1772},{},[1773,1778],{"type":18,"tag":51,"props":1774,"children":1776},{"className":1775},[],[1777],{"type":28,"value":1350},{"type":28,"value":1352},{"type":18,"tag":1331,"props":1780,"children":1781},{},[1782,1787],{"type":18,"tag":51,"props":1783,"children":1785},{"className":1784},[],[1786],{"type":28,"value":1361},{"type":28,"value":1788},"：可选参数，表示标志位，用于控制匹配方式，如是否区分字母大小写。常用的标志如表5所示。",{"type":18,"tag":30,"props":1790,"children":1791},{},[1792],{"type":28,"value":1793},"例如，搜索第一个以“mr_”开头的字符串，不区分字母大小写，代码如下：",{"type":18,"tag":103,"props":1795,"children":1797},{"code":1796,"language":106,"meta":7,"className":107,"style":7},"import re\npattern = r'mr_\\w+'                              # 模式字符串\nstring = 'MR_SHOP mr_shop'                      # 要匹配的字符串\nmatch = re.search(pattern,string,re.I)          # 搜索字符串，不区分大小写\nprint(match)                                      # 输出匹配结果\nstring = '项目名称MR_SHOP mr_shop'\nmatch = re.search(pattern,string,re.I)          # 搜索字符串，不区分大小写\nprint(match)                                      # 输出匹配结果\n",[1798],{"type":18,"tag":51,"props":1799,"children":1800},{"__ignoreMap":7},[1801,1808,1816,1823,1831,1839,1846,1853],{"type":18,"tag":113,"props":1802,"children":1803},{"class":115,"line":116},[1804],{"type":18,"tag":113,"props":1805,"children":1806},{},[1807],{"type":28,"value":1241},{"type":18,"tag":113,"props":1809,"children":1810},{"class":115,"line":1477},[1811],{"type":18,"tag":113,"props":1812,"children":1813},{},[1814],{"type":28,"value":1815},"pattern = r'mr_\\w+'                              # 模式字符串\n",{"type":18,"tag":113,"props":1817,"children":1818},{"class":115,"line":1486},[1819],{"type":18,"tag":113,"props":1820,"children":1821},{},[1822],{"type":28,"value":1648},{"type":18,"tag":113,"props":1824,"children":1825},{"class":115,"line":1495},[1826],{"type":18,"tag":113,"props":1827,"children":1828},{},[1829],{"type":28,"value":1830},"match = re.search(pattern,string,re.I)          # 搜索字符串，不区分大小写\n",{"type":18,"tag":113,"props":1832,"children":1833},{"class":115,"line":1504},[1834],{"type":18,"tag":113,"props":1835,"children":1836},{},[1837],{"type":28,"value":1838},"print(match)                                      # 输出匹配结果\n",{"type":18,"tag":113,"props":1840,"children":1841},{"class":115,"line":1513},[1842],{"type":18,"tag":113,"props":1843,"children":1844},{},[1845],{"type":28,"value":1519},{"type":18,"tag":113,"props":1847,"children":1848},{"class":115,"line":1522},[1849],{"type":18,"tag":113,"props":1850,"children":1851},{},[1852],{"type":28,"value":1830},{"type":18,"tag":113,"props":1854,"children":1855},{"class":115,"line":1530},[1856],{"type":18,"tag":113,"props":1857,"children":1858},{},[1859],{"type":28,"value":1838},{"type":18,"tag":30,"props":1861,"children":1862},{},[1863],{"type":28,"value":1540},{"type":18,"tag":103,"props":1865,"children":1867},{"code":1866},"\u003C_sre.SRE_Match object; span=(0, 7), match='MR_SHOP'>\n\u003C_sre.SRE_Match object; span=(4, 11), match='MR_SHOP'>\n",[1868],{"type":18,"tag":51,"props":1869,"children":1870},{"__ignoreMap":7},[1871],{"type":28,"value":1866},{"type":18,"tag":30,"props":1873,"children":1874},{},[1875],{"type":28,"value":1876},"从上面的运行结果中可以看出，search()方法不仅仅是在字符串的起始位置搜索，其他位置有符合的匹配也可以进行搜索。",{"type":18,"tag":76,"props":1878,"children":1880},{"id":1879},"使用findall方法进行匹配",[1881],{"type":28,"value":1882},"使用findall()方法进行匹配",{"type":18,"tag":30,"props":1884,"children":1885},{},[1886,1891,1892,1897,1899,1904,1906,1911],{"type":18,"tag":51,"props":1887,"children":1889},{"className":1888},[],[1890],{"type":28,"value":1218},{"type":28,"value":1725},{"type":18,"tag":36,"props":1893,"children":1894},{},[1895],{"type":28,"value":1896},"在整个字符串中搜索所有符合正则表达式的字符串",{"type":28,"value":1898},"，并",{"type":18,"tag":36,"props":1900,"children":1901},{},[1902],{"type":28,"value":1903},"以列表的形式返回",{"type":28,"value":1905},"。如果匹配成功，则返回包含匹配结构的列表，否则返回空列表。",{"type":18,"tag":51,"props":1907,"children":1909},{"className":1908},[],[1910],{"type":28,"value":1218},{"type":28,"value":1739},{"type":18,"tag":103,"props":1913,"children":1915},{"code":1914,"language":106,"meta":7,"className":107,"style":7},"re.findall(pattern, string, [flags])\n",[1916],{"type":18,"tag":51,"props":1917,"children":1918},{"__ignoreMap":7},[1919],{"type":18,"tag":113,"props":1920,"children":1921},{"class":115,"line":116},[1922],{"type":18,"tag":113,"props":1923,"children":1924},{},[1925],{"type":28,"value":1914},{"type":18,"tag":30,"props":1927,"children":1928},{},[1929],{"type":28,"value":1325},{"type":18,"tag":1327,"props":1931,"children":1932},{},[1933,1942,1951],{"type":18,"tag":1331,"props":1934,"children":1935},{},[1936,1941],{"type":18,"tag":51,"props":1937,"children":1939},{"className":1938},[],[1940],{"type":28,"value":1339},{"type":28,"value":1341},{"type":18,"tag":1331,"props":1943,"children":1944},{},[1945,1950],{"type":18,"tag":51,"props":1946,"children":1948},{"className":1947},[],[1949],{"type":28,"value":1350},{"type":28,"value":1352},{"type":18,"tag":1331,"props":1952,"children":1953},{},[1954,1959],{"type":18,"tag":51,"props":1955,"children":1957},{"className":1956},[],[1958],{"type":28,"value":1361},{"type":28,"value":1788},{"type":18,"tag":30,"props":1961,"children":1962},{},[1963],{"type":28,"value":1964},"例如，搜索以“mr_”开头的字符串，代码如下：",{"type":18,"tag":103,"props":1966,"children":1968},{"code":1967,"language":106,"meta":7,"className":107,"style":7},"import re\npattern = r'mr_\\w+'                            # 模式字符串\nstring = 'MR_SHOP mr_shop'                    # 要匹配的字符串\nmatch = re.findall(pattern,string,re.I)      # 搜索字符串，不区分大小写\nprint(match)                                  # 输出匹配结果\nstring = '项目名称MR_SHOP mr_shop'\nmatch = re.findall(pattern,string)            # 搜索字符串，区分大小写\nprint(match)                                 # 输出匹配结果\n",[1969],{"type":18,"tag":51,"props":1970,"children":1971},{"__ignoreMap":7},[1972,1979,1987,1995,2003,2010,2017,2025],{"type":18,"tag":113,"props":1973,"children":1974},{"class":115,"line":116},[1975],{"type":18,"tag":113,"props":1976,"children":1977},{},[1978],{"type":28,"value":1241},{"type":18,"tag":113,"props":1980,"children":1981},{"class":115,"line":1477},[1982],{"type":18,"tag":113,"props":1983,"children":1984},{},[1985],{"type":28,"value":1986},"pattern = r'mr_\\w+'                            # 模式字符串\n",{"type":18,"tag":113,"props":1988,"children":1989},{"class":115,"line":1486},[1990],{"type":18,"tag":113,"props":1991,"children":1992},{},[1993],{"type":28,"value":1994},"string = 'MR_SHOP mr_shop'                    # 要匹配的字符串\n",{"type":18,"tag":113,"props":1996,"children":1997},{"class":115,"line":1495},[1998],{"type":18,"tag":113,"props":1999,"children":2000},{},[2001],{"type":28,"value":2002},"match = re.findall(pattern,string,re.I)      # 搜索字符串，不区分大小写\n",{"type":18,"tag":113,"props":2004,"children":2005},{"class":115,"line":1504},[2006],{"type":18,"tag":113,"props":2007,"children":2008},{},[2009],{"type":28,"value":1510},{"type":18,"tag":113,"props":2011,"children":2012},{"class":115,"line":1513},[2013],{"type":18,"tag":113,"props":2014,"children":2015},{},[2016],{"type":28,"value":1519},{"type":18,"tag":113,"props":2018,"children":2019},{"class":115,"line":1522},[2020],{"type":18,"tag":113,"props":2021,"children":2022},{},[2023],{"type":28,"value":2024},"match = re.findall(pattern,string)            # 搜索字符串，区分大小写\n",{"type":18,"tag":113,"props":2026,"children":2027},{"class":115,"line":1530},[2028],{"type":18,"tag":113,"props":2029,"children":2030},{},[2031],{"type":28,"value":2032},"print(match)                                 # 输出匹配结果\n",{"type":18,"tag":30,"props":2034,"children":2035},{},[2036],{"type":28,"value":1540},{"type":18,"tag":103,"props":2038,"children":2040},{"code":2039},"['MR_SHOP', 'mr_shop']\n['mr_shop']\n",[2041],{"type":18,"tag":51,"props":2042,"children":2043},{"__ignoreMap":7},[2044],{"type":28,"value":2039},{"type":18,"tag":30,"props":2046,"children":2047},{},[2048],{"type":28,"value":2049},"如果在指定的模式字符串中，包含分组，则返回与分组匹配的文本列表。例如，",{"type":18,"tag":103,"props":2051,"children":2053},{"code":2052,"language":106,"meta":7,"className":107,"style":7},"import re\npattern = r'[1-9]{1,3}(\\.[0-9]{1,3}){3}'     # 模式字符串\nstr1 = '127.0.0.1 192.168.1.66'               # 要配置的字符串\nmatch = re.findall(pattern,str1)              # 进行模式匹配\nprint(match)\n",[2054],{"type":18,"tag":51,"props":2055,"children":2056},{"__ignoreMap":7},[2057,2064,2072,2080,2088],{"type":18,"tag":113,"props":2058,"children":2059},{"class":115,"line":116},[2060],{"type":18,"tag":113,"props":2061,"children":2062},{},[2063],{"type":28,"value":1241},{"type":18,"tag":113,"props":2065,"children":2066},{"class":115,"line":1477},[2067],{"type":18,"tag":113,"props":2068,"children":2069},{},[2070],{"type":28,"value":2071},"pattern = r'[1-9]{1,3}(\\.[0-9]{1,3}){3}'     # 模式字符串\n",{"type":18,"tag":113,"props":2073,"children":2074},{"class":115,"line":1486},[2075],{"type":18,"tag":113,"props":2076,"children":2077},{},[2078],{"type":28,"value":2079},"str1 = '127.0.0.1 192.168.1.66'               # 要配置的字符串\n",{"type":18,"tag":113,"props":2081,"children":2082},{"class":115,"line":1495},[2083],{"type":18,"tag":113,"props":2084,"children":2085},{},[2086],{"type":28,"value":2087},"match = re.findall(pattern,str1)              # 进行模式匹配\n",{"type":18,"tag":113,"props":2089,"children":2090},{"class":115,"line":1504},[2091],{"type":18,"tag":113,"props":2092,"children":2093},{},[2094],{"type":28,"value":2095},"print(match)\n",{"type":18,"tag":30,"props":2097,"children":2098},{},[2099],{"type":28,"value":2100},"上面代码的执行结果如下：",{"type":18,"tag":103,"props":2102,"children":2104},{"code":2103},"['.1', '.66']\n",[2105],{"type":18,"tag":51,"props":2106,"children":2107},{"__ignoreMap":7},[2108],{"type":28,"value":2103},{"type":18,"tag":30,"props":2110,"children":2111},{},[2112,2114,2119],{"type":28,"value":2113},"从上面的结果中可以看出，并没有得到匹配的IP地址，这是因为在模式字符串中出现了分组，所以得到的结果是根据分组进行匹配的结果，即",{"type":18,"tag":51,"props":2115,"children":2117},{"className":2116},[],[2118],{"type":28,"value":1073},{"type":28,"value":2120},"匹配的结果。如果想获取整个模式字符串的匹配，可以将整个模式字符串使用一对小括号进行分组，然后在获取结果时，只取返回值列表的每个元素（是一个元组）的第1个元素。代码如下：",{"type":18,"tag":103,"props":2122,"children":2124},{"code":2123,"language":106,"meta":7,"className":107,"style":7},"import re\npattern = r'([1-9]{1,3}(\\.[0-9]{1,3}){3})'         # 模式字符串\nstr1 = '127.0.0.1 192.168.1.66'                    # 要配置的字符串\nmatch = re.findall(pattern,str1)                   # 进行模式匹配\nfor item in match:\n    print(item[0])\n",[2125],{"type":18,"tag":51,"props":2126,"children":2127},{"__ignoreMap":7},[2128,2135,2143,2151,2159,2167],{"type":18,"tag":113,"props":2129,"children":2130},{"class":115,"line":116},[2131],{"type":18,"tag":113,"props":2132,"children":2133},{},[2134],{"type":28,"value":1241},{"type":18,"tag":113,"props":2136,"children":2137},{"class":115,"line":1477},[2138],{"type":18,"tag":113,"props":2139,"children":2140},{},[2141],{"type":28,"value":2142},"pattern = r'([1-9]{1,3}(\\.[0-9]{1,3}){3})'         # 模式字符串\n",{"type":18,"tag":113,"props":2144,"children":2145},{"class":115,"line":1486},[2146],{"type":18,"tag":113,"props":2147,"children":2148},{},[2149],{"type":28,"value":2150},"str1 = '127.0.0.1 192.168.1.66'                    # 要配置的字符串\n",{"type":18,"tag":113,"props":2152,"children":2153},{"class":115,"line":1495},[2154],{"type":18,"tag":113,"props":2155,"children":2156},{},[2157],{"type":28,"value":2158},"match = re.findall(pattern,str1)                   # 进行模式匹配\n",{"type":18,"tag":113,"props":2160,"children":2161},{"class":115,"line":1504},[2162],{"type":18,"tag":113,"props":2163,"children":2164},{},[2165],{"type":28,"value":2166},"for item in match:\n",{"type":18,"tag":113,"props":2168,"children":2169},{"class":115,"line":1513},[2170],{"type":18,"tag":113,"props":2171,"children":2172},{},[2173],{"type":28,"value":2174},"    print(item[0])\n",{"type":18,"tag":30,"props":2176,"children":2177},{},[2178],{"type":28,"value":1540},{"type":18,"tag":103,"props":2180,"children":2182},{"code":2181,"language":106,"meta":7,"className":107,"style":7},"127.0.0.1\n192.168.1.66\n",[2183],{"type":18,"tag":51,"props":2184,"children":2185},{"__ignoreMap":7},[2186,2194],{"type":18,"tag":113,"props":2187,"children":2188},{"class":115,"line":116},[2189],{"type":18,"tag":113,"props":2190,"children":2191},{},[2192],{"type":28,"value":2193},"127.0.0.1\n",{"type":18,"tag":113,"props":2195,"children":2196},{"class":115,"line":1477},[2197],{"type":18,"tag":113,"props":2198,"children":2199},{},[2200],{"type":28,"value":2201},"192.168.1.66\n",{"type":18,"tag":23,"props":2203,"children":2205},{"id":2204},"替换字符串",[2206],{"type":28,"value":2204},{"type":18,"tag":30,"props":2208,"children":2209},{},[2210,2216,2217,2222],{"type":18,"tag":51,"props":2211,"children":2213},{"className":2212},[],[2214],{"type":28,"value":2215},"sub()",{"type":28,"value":1725},{"type":18,"tag":36,"props":2218,"children":2219},{},[2220],{"type":28,"value":2221},"实现字符串替换",{"type":28,"value":2223},"，语法格式如下：",{"type":18,"tag":103,"props":2225,"children":2227},{"code":2226,"language":106,"meta":7,"className":107,"style":7},"re.sub(pattern, repl, string, count, flags)\n",[2228],{"type":18,"tag":51,"props":2229,"children":2230},{"__ignoreMap":7},[2231],{"type":18,"tag":113,"props":2232,"children":2233},{"class":115,"line":116},[2234],{"type":18,"tag":113,"props":2235,"children":2236},{},[2237],{"type":28,"value":2226},{"type":18,"tag":30,"props":2239,"children":2240},{},[2241],{"type":28,"value":1325},{"type":18,"tag":1327,"props":2243,"children":2244},{},[2245,2254,2265,2275,2286],{"type":18,"tag":1331,"props":2246,"children":2247},{},[2248,2253],{"type":18,"tag":51,"props":2249,"children":2251},{"className":2250},[],[2252],{"type":28,"value":1339},{"type":28,"value":1341},{"type":18,"tag":1331,"props":2255,"children":2256},{},[2257,2263],{"type":18,"tag":51,"props":2258,"children":2260},{"className":2259},[],[2261],{"type":28,"value":2262},"repl",{"type":28,"value":2264},"：表示替换的字符串。",{"type":18,"tag":1331,"props":2266,"children":2267},{},[2268,2273],{"type":18,"tag":51,"props":2269,"children":2271},{"className":2270},[],[2272],{"type":28,"value":1350},{"type":28,"value":2274},"：表示要被查找替换的原始字符串。",{"type":18,"tag":1331,"props":2276,"children":2277},{},[2278,2284],{"type":18,"tag":51,"props":2279,"children":2281},{"className":2280},[],[2282],{"type":28,"value":2283},"count",{"type":28,"value":2285},"：可选参数，表示模式匹配后替换的最大次数，默认值为0，表示替换所有的匹配。",{"type":18,"tag":1331,"props":2287,"children":2288},{},[2289,2294],{"type":18,"tag":51,"props":2290,"children":2292},{"className":2291},[],[2293],{"type":28,"value":1361},{"type":28,"value":1788},{"type":18,"tag":30,"props":2296,"children":2297},{},[2298],{"type":28,"value":2299},"例如，隐藏中奖信息中的手机号码，代码如下：",{"type":18,"tag":103,"props":2301,"children":2303},{"code":2302,"language":106,"meta":7,"className":107,"style":7},"import re\npattern = r'1[34578]\\d{9}'                           # 定义要替换的模式字符串\nstring = '中奖号码为：84978981 联系电话为：13611111111'\nresult = re.sub(pattern,'1XXXXXXXXXX',string)      # 替换字符串\nprint(result)\n",[2304],{"type":18,"tag":51,"props":2305,"children":2306},{"__ignoreMap":7},[2307,2314,2322,2330,2338],{"type":18,"tag":113,"props":2308,"children":2309},{"class":115,"line":116},[2310],{"type":18,"tag":113,"props":2311,"children":2312},{},[2313],{"type":28,"value":1241},{"type":18,"tag":113,"props":2315,"children":2316},{"class":115,"line":1477},[2317],{"type":18,"tag":113,"props":2318,"children":2319},{},[2320],{"type":28,"value":2321},"pattern = r'1[34578]\\d{9}'                           # 定义要替换的模式字符串\n",{"type":18,"tag":113,"props":2323,"children":2324},{"class":115,"line":1486},[2325],{"type":18,"tag":113,"props":2326,"children":2327},{},[2328],{"type":28,"value":2329},"string = '中奖号码为：84978981 联系电话为：13611111111'\n",{"type":18,"tag":113,"props":2331,"children":2332},{"class":115,"line":1495},[2333],{"type":18,"tag":113,"props":2334,"children":2335},{},[2336],{"type":28,"value":2337},"result = re.sub(pattern,'1XXXXXXXXXX',string)      # 替换字符串\n",{"type":18,"tag":113,"props":2339,"children":2340},{"class":115,"line":1504},[2341],{"type":18,"tag":113,"props":2342,"children":2343},{},[2344],{"type":28,"value":2345},"print(result)\n",{"type":18,"tag":30,"props":2347,"children":2348},{},[2349],{"type":28,"value":1540},{"type":18,"tag":103,"props":2351,"children":2353},{"code":2352},"中奖号码为：84978981 联系电话为：1XXXXXXXXXX\n",[2354],{"type":18,"tag":51,"props":2355,"children":2356},{"__ignoreMap":7},[2357],{"type":28,"value":2352},{"type":18,"tag":23,"props":2359,"children":2361},{"id":2360},"分割字符串",[2362],{"type":28,"value":2360},{"type":18,"tag":30,"props":2364,"children":2365},{},[2366,2372,2373,2378,2379,2383,2385,2390,2392,2397],{"type":18,"tag":51,"props":2367,"children":2369},{"className":2368},[],[2370],{"type":28,"value":2371},"split()",{"type":28,"value":1725},{"type":18,"tag":36,"props":2374,"children":2375},{},[2376],{"type":28,"value":2377},"实现根据正则表达式分割字符串",{"type":28,"value":1898},{"type":18,"tag":36,"props":2380,"children":2381},{},[2382],{"type":28,"value":1903},{"type":28,"value":2384},"。其作用同字符串对象的",{"type":18,"tag":51,"props":2386,"children":2388},{"className":2387},[],[2389],{"type":28,"value":2371},{"type":28,"value":2391},"方法类似，所不同的就是分割字符由模式字符串指定。",{"type":18,"tag":51,"props":2393,"children":2395},{"className":2394},[],[2396],{"type":28,"value":2371},{"type":28,"value":1739},{"type":18,"tag":103,"props":2399,"children":2401},{"code":2400,"language":106,"meta":7,"className":107,"style":7},"re.split(pattern, string, [maxsplit], [flags])\n",[2402],{"type":18,"tag":51,"props":2403,"children":2404},{"__ignoreMap":7},[2405],{"type":18,"tag":113,"props":2406,"children":2407},{"class":115,"line":116},[2408],{"type":18,"tag":113,"props":2409,"children":2410},{},[2411],{"type":28,"value":2400},{"type":18,"tag":30,"props":2413,"children":2414},{},[2415],{"type":28,"value":1325},{"type":18,"tag":1327,"props":2417,"children":2418},{},[2419,2428,2437,2448],{"type":18,"tag":1331,"props":2420,"children":2421},{},[2422,2427],{"type":18,"tag":51,"props":2423,"children":2425},{"className":2424},[],[2426],{"type":28,"value":1339},{"type":28,"value":1341},{"type":18,"tag":1331,"props":2429,"children":2430},{},[2431,2436],{"type":18,"tag":51,"props":2432,"children":2434},{"className":2433},[],[2435],{"type":28,"value":1350},{"type":28,"value":1352},{"type":18,"tag":1331,"props":2438,"children":2439},{},[2440,2446],{"type":18,"tag":51,"props":2441,"children":2443},{"className":2442},[],[2444],{"type":28,"value":2445},"maxsplit",{"type":28,"value":2447},"：可选参数，表示最大的拆分次数。",{"type":18,"tag":1331,"props":2449,"children":2450},{},[2451,2456],{"type":18,"tag":51,"props":2452,"children":2454},{"className":2453},[],[2455],{"type":28,"value":1361},{"type":28,"value":1788},{"type":18,"tag":30,"props":2458,"children":2459},{},[2460],{"type":28,"value":2461},"例如，从给定的URL地址中提取出请求地址和各个参数，代码如下：",{"type":18,"tag":103,"props":2463,"children":2465},{"code":2464,"language":106,"meta":7,"className":107,"style":7},"import re\npattern = r'[?|&]'                         # 定义分割符\nurl = 'http:\u002F\u002Fwww.mingrisoft.com\u002Flogin.jsp?username=\"mr\"&pwd=\"mrsoft\"'\nresult = re.split(pattern,url)              # 分割字符串\nprint(result)\n",[2466],{"type":18,"tag":51,"props":2467,"children":2468},{"__ignoreMap":7},[2469,2476,2484,2492,2500],{"type":18,"tag":113,"props":2470,"children":2471},{"class":115,"line":116},[2472],{"type":18,"tag":113,"props":2473,"children":2474},{},[2475],{"type":28,"value":1241},{"type":18,"tag":113,"props":2477,"children":2478},{"class":115,"line":1477},[2479],{"type":18,"tag":113,"props":2480,"children":2481},{},[2482],{"type":28,"value":2483},"pattern = r'[?|&]'                         # 定义分割符\n",{"type":18,"tag":113,"props":2485,"children":2486},{"class":115,"line":1486},[2487],{"type":18,"tag":113,"props":2488,"children":2489},{},[2490],{"type":28,"value":2491},"url = 'http:\u002F\u002Fwww.mingrisoft.com\u002Flogin.jsp?username=\"mr\"&pwd=\"mrsoft\"'\n",{"type":18,"tag":113,"props":2493,"children":2494},{"class":115,"line":1495},[2495],{"type":18,"tag":113,"props":2496,"children":2497},{},[2498],{"type":28,"value":2499},"result = re.split(pattern,url)              # 分割字符串\n",{"type":18,"tag":113,"props":2501,"children":2502},{"class":115,"line":1504},[2503],{"type":18,"tag":113,"props":2504,"children":2505},{},[2506],{"type":28,"value":2345},{"type":18,"tag":30,"props":2508,"children":2509},{},[2510],{"type":28,"value":1540},{"type":18,"tag":103,"props":2512,"children":2514},{"code":2513},"['http:\u002F\u002Fwww.mingrisoft.com\u002Flogin.jsp', 'username=\"mr\"', 'pwd=\"mrsoft\"']\n",[2515],{"type":18,"tag":51,"props":2516,"children":2517},{"__ignoreMap":7},[2518],{"type":28,"value":2513},{"type":18,"tag":2520,"props":2521,"children":2522},"style",{},[2523],{"type":28,"value":2524},"html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}",{"title":7,"searchDepth":1477,"depth":1477,"links":2526},[2527,2538,2543,2544],{"id":25,"depth":1477,"text":25,"children":2528},[2529,2530,2531,2532,2533,2534,2535,2536,2537],{"id":78,"depth":1486,"text":78},{"id":193,"depth":1486,"text":193},{"id":522,"depth":1486,"text":522},{"id":732,"depth":1486,"text":732},{"id":817,"depth":1486,"text":817},{"id":867,"depth":1486,"text":867},{"id":909,"depth":1486,"text":909},{"id":989,"depth":1486,"text":989},{"id":1078,"depth":1486,"text":1081},{"id":1176,"depth":1477,"text":1176,"children":2539},[2540,2541,2542],{"id":1293,"depth":1486,"text":1296},{"id":1712,"depth":1486,"text":1715},{"id":1879,"depth":1486,"text":1882},{"id":2204,"depth":1477,"text":2204},{"id":2360,"depth":1477,"text":2360},"markdown","content:blog:2025-07-11-python-使用正则表达式.md","content","blog\u002F2025-07-11-python-使用正则表达式.md","blog\u002F2025-07-11-python-使用正则表达式","md",1780801018414]