{"id":450,"date":"2017-02-07T23:14:47","date_gmt":"2017-02-07T15:14:47","guid":{"rendered":"http:\/\/www.rainweb.site\/?p=450"},"modified":"2023-08-07T20:57:30","modified_gmt":"2023-08-07T12:57:30","slug":"python-article35","status":"publish","type":"post","link":"http:\/\/rain1024.com\/index.php\/2017\/02\/07\/python-article35\/","title":{"rendered":"Python\u6b63\u5219\u8868\u8fbe\u5f0f\u51fd\u6570\u5b9e\u4f8b\u548c\u5e38\u89c1\u7b26\u53f7\u7684\u4f7f\u7528"},"content":{"rendered":"<h2>\u4eca\u5929\u8981\u8bf4\u7684\u662f\u6b63\u5219\u8868\u8fbe\u5f0f\uff01\uff01\uff01<\/h2>\n<h3>\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u987e\u540d\u601d\u4e49\u662f\u5bf9\u8868\u8fbe\u5f0f\u7684\u4e00\u79cd\u7b5b\u9009\uff0c\u5904\u7406\uff0c\u5224\u65ad\u3002\u5f53\u7136\u6211\u4e5f\u53ea\u662f\u4e00\u77e5\u534a\u89e3\uff0c\u5076\u5c14\u5728\u722c\u866b\u91cc\u4f1a\u7528\u4e00\u4e9b\uff0c\u4f46\u90fd\u662f\u5f88\u7b80\u5355\u7684\u8868\u8fbe\u5f0f\uff0c\u6240\u4ee5\u4eca\u5929\u6d45\u8c08\u4e00\u4e0bpython\u4e2dre\u6a21\u5757<\/h3>\n<h3>\u5148\u8bf4\u7406\u8bba\u7684\uff0cre\u5e93\u91cc\u5305\u542b\u4e86\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u5904\u7406\u51fd\u6570<\/h3>\n<ol>\n<li>re.match(pattem,string,flags)\n<p>re.match\u662f\u4ece\u5b57\u7b26\u4e32\u7684\u5f00\u59cb\u53bb\u5339\u914d\u4e00\u4e2a\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f\uff0c\u51fd\u6570\u6709\u4e09\u4e2a\u53c2\u6570\uff0c\u7b2c\u4e00\u4e2a\u662f\u8981\u5339\u914d\u7684\u6b63\u5219\u8868\u8fbe\u5f0f<\/p>\n<p>\u7b2c\u4e8c\u4e2a\u662f\u8981\u5339\u914d\u7684\u5b57\u7b26\u4e32\u6587\u672c<\/p>\n<p>\u7b2c\u4e09\u4e2a\u662f\u63a7\u5236\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u5339\u914d\u65b9\u5f0f<\/p>\n<\/li>\n<li>\n<p>re.search(pattem,string,flags)<\/p>\n<p>re.search\u4f1a\u5728\u5b57\u7b26\u4e32\u4e2d\u67e5\u627e\u5230\u7b2c\u4e00\u4e2a\u5339\u914d\u6a21\u5f0f\u7136\u540e\u8fd4\u56de\u53c2\u6570\u4e0ere.match\u76f8\u540c<\/p>\n<\/li>\n<li>\n<p>re.sub(pattern,repl,string,count)<\/p>\n<p>re.sub\u7528\u4e8e\u66ff\u6362\u5b57\u7b26\u4e32\u4e2d\u7684\u5339\u914d\u9879\uff0cpattern\u662f\u8981\u5339\u914d\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c<\/p>\n<p>repl\u662f\u8981\u66ff\u6362\u7684\u5b57\u7b26\u4e32\uff0cstring\u8868\u793a\u5b57\u7b26\u4e32\u6587\u672ccount\u8868\u793a\u8981\u66ff\u6362\u7684\u4e2a\u6570\uff0c\u53ef\u4e0d\u5199\uff0c\u9ed8\u8ba4\u5168\u90e8\u66ff\u6362re.split\uff08pattern\uff0cstring\uff09<\/p>\n<\/li>\n<li>\n<p>re.findall\uff08pattern\uff0cstring\uff09\uff08\u6211\u6700\u5e38\u7528\uff09<\/p>\n<p>\u6309\u7167\u5339\u914d\u6a21\u5f0f\u5206\u5272\u5b57\u7b26\u4e32\u4e3a\u4e00\u4e2a\u5217\u8868\u83b7\u53d6\u6240\u6709\u5339\u914d\u7684\u5b57\u7b26\u4e32<\/p>\n<\/li>\n<li>\n<p>re.compile\uff08\uff09<\/p>\n<p>\u53ef\u4ee5\u628a\u6b63\u5219\u8868\u8fbe\u5f0f\u7f16\u8bd1\u6210\u4e00\u4e2a\u6b63\u5219\u8868\u8fbe\u5f0f\u5bf9\u8c61<\/p>\n<\/li>\n<\/ol>\n<pre><code class=\"python\">import re\ntext = \"JGood is a handsome boy, he is cool, clever, and so on...\"\nregex = re.compile(r'\\woo\\w')\nprint regex.findall(text)\u00a0\u00a0 #\u67e5\u627e\u6240\u6709\u5305\u542b'oo'\u7684\u5355\u8bcd\nprint regex.sub(lambda m: '[' + m.group(0) + ']', text) #\u5c06\u5b57\u7b26\u4e32\u4e2d\u542b\u6709'oo'\u7684\u5355\u8bcd\u7528[]\u62ec\u8d77\u6765\u3002\n<\/code><\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/blog-1252406596.costj.myqcloud.com\/blog\/python22.png\" width=\"631\" height=\"471\" class=\"alignnone size-medium\" \/><\/p>\n<h2>\u4e0b\u9762\u8bf4\u4e00\u8bf4\u6b63\u5219\u8868\u8fbe\u5f0f\u4e2d\u51e0\u4e2a\u5e38\u7528\u7b26\u53f7<\/h2>\n<h4>1. \u4f7f\u7528\u2018 . \u2019\u7b26\u53f7<\/h4>\n<pre><code>\u4f7f\u7528\u51e0\u4e2a\u2018 . \u2019\u5219\u5f80\u540e\u5339\u914d\u51e0\u4e2a\u5b57\u7b26\nimport re\ncode = 'abcahf123afr'\nb = re.findall('a..',code)\nprint b\n&gt;&gt;&gt;['abc', 'ahf', 'afr']\n<\/code><\/pre>\n<h4>2. \u4f7f\u7528\u2018 .* \u2019 \u548c \u2018 .*\uff1f \u2019\u8d2a\u5a6a\u6a21\u5f0f\u4e0e\u975e\u8d2a\u5a6a\u6a21\u5f0f<\/h4>\n<pre><code>import re\ncode = 'abcahf123afr'\nb = re.findall('a.*a',code)\nprint b\n&gt;&gt;&gt;['abcahf123a']  #\u8d2a\u5a6a\u6a21\u5f0f\u5339\u914d\u5f00\u59cb\u548c\u7ed3\u675f\u5b57\u7b26\u4e2d\u95f4\u5185\u5bb9\u65f6\uff0c\u5185\u5bb9\u8d8a\u591a\u8d8a\u597d\uff0c\u76f4\u5230\u5339\u914d\u5230\u6700\u540e\u4e00\u4e2a\u76f8\u540c\u7684\u5b57\u7b26\u3002\nb = re.findall('a.*?a',code)\nprint b\n&gt;&gt;&gt;['abca']  #\u975e\u8d2a\u5a6a\u6a21\u5f0f\u5339\u914d\u5f00\u59cb\u548c\u7ed3\u675f\u5b57\u7b26\u4e2d\u95f4\u5185\u5bb9\u65f6\uff0c\u5185\u5bb9\u8d8a\u5c11\u8d8a\u597d\uff0c\u4e00\u65e6\u5339\u914d\u5230\u76f8\u540c\u7684\u5b57\u7b26\u5c31\u4f1a\u7ed3\u675f\u3002\n<\/code><\/pre>\n<h4>3. \u62ec\u53f7\u7684\u4f7f\u7528\u53ef\u4ee5\u63d0\u53d6\u60f3\u8981\u7684\u5185\u5bb9<\/h4>\n<pre><code>import re\ncode = 'abcahf123afr'\nb = re.findall('a(.*a)',code)\nprint b\nb = re.findall('a(.*?)a',code)\nprint b\n&gt;&gt;&gt;['bcahf123a']\n&gt;&gt;&gt;['bc']\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u4eca\u5929\u8981\u8bf4\u7684\u662f\u6b63\u5219\u8868\u8fbe\u5f0f\uff01\uff01\uff01 \u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u987e\u540d\u601d\u4e49\u662f\u5bf9\u8868\u8fbe\u5f0f\u7684\u4e00\u79cd\u7b5b\u9009\uff0c\u5904\u7406\uff0c\u5224\u65ad\u3002\u5f53\u7136\u6211\u4e5f\u53ea\u662f\u4e00\u77e5\u534a\u89e3\uff0c\u5076\u5c14\u2026 <span class=\"read-more\"><a href=\"http:\/\/rain1024.com\/index.php\/2017\/02\/07\/python-article35\/\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":451,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"aside","meta":{"footnotes":""},"categories":[15],"tags":[58,79],"class_list":["post-450","post","type-post","status-publish","format-aside","has-post-thumbnail","hentry","category-python","tag-python","tag-regular","post_format-post-format-aside"],"_links":{"self":[{"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/posts\/450","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/comments?post=450"}],"version-history":[{"count":1,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/posts\/450\/revisions"}],"predecessor-version":[{"id":1505,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/posts\/450\/revisions\/1505"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/media\/451"}],"wp:attachment":[{"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/media?parent=450"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/categories?post=450"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/tags?post=450"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}