{"id":1751,"date":"2020-07-01T04:14:54","date_gmt":"2020-06-30T20:14:54","guid":{"rendered":"http:\/\/39.97.231.10\/?p=1751"},"modified":"2020-07-01T04:14:54","modified_gmt":"2020-06-30T20:14:54","slug":"python-%e7%9a%84%e6%ad%a3%e5%88%99%e8%a1%a8%e8%be%be%e5%bc%8f","status":"publish","type":"post","link":"http:\/\/www.weijing.co\/?p=1751","title":{"rendered":"Python \u7684\u6b63\u5219\u8868\u8fbe\u5f0f"},"content":{"rendered":"<div>\n\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u662f\u67e5\u627e\u6587\u672c\u7684\u4e00\u79cd\u63cf\u8ff0\u65b9\u6cd5\u3002\u4f8b\u5982\uff0c \u4e00\u4e2a\u6b63\u5219\u8868\u8fbe\u5f0f\u4e3a \\\\d\uff0c \u8868\u793a\u4e00\u4f4d\u6570\u5b57\u5b57\u7b26\uff0c\u5373\u4e00\u4f4d 0 \u5230 9 \u4e4b\u95f4\u7684\u4efb\u610f\u6570\u5b57\u3002<\/p>\n<div>\n<pre>import re\nphone_num_regex=re.compile(r\\'\\\\d\\\\d\\\\d\\\\d-\\\\d\\\\d\\\\d\\\\d\\\\d\\\\d\\\\d\\\\d\\')\nmo=phone_num_regex.search(\\'\u6211\u7684\u8054\u7cfb\u53f7\u7801\u4e3a\uff1a 0591-83822032.\\')\nprint(\\'\u67e5\u5230\u7684\u8054\u7cfb\u53f7\u7801: \\'+mo.group())\n<\/pre>\n<\/div>\n<p>\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n<blockquote><p>\u67e5\u5230\u7684\u8054\u7cfb\u53f7\u7801: 0591-83822032<\/p><\/blockquote>\n<p>\u6211\u4eec\u5bf9\u4e0a\u8ff0\u793a\u4f8b\u8fdb\u884c\u5206\u6790\u3002<br \/>\n\uff081\uff09\u5bfc\u5165\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5757<br \/>\nPython \u4e2d\u6240\u6709\u6b63\u5219\u8868\u8fbe\u5f0f\u51fd\u6570\u90fd\u5728 re \u6a21\u5757\u4e2d\uff0c\u6240\u4ee5\u6211\u4eec\u9996\u5148\u5c06\u5176\u5bfc\u5165\u3002<\/p>\n<div>\n<pre>import re\n<\/pre>\n<\/div>\n<p>\uff082\uff09\u521b\u5efa\u6b63\u5219\u8868\u8fbe\u5f0f\u5bf9\u8c61<br \/>\n\u5411 re.compile() \u4f20\u5165\u4e00\u4e2a\u5b57\u7b26\u4e32\u503c\uff0c\u8868\u793a\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u5b83\u5c06\u8fd4\u56de\u4e00\u4e2a Regex \u6a21\u5f0f\u5bf9\u8c61\u3002<\/p>\n<div>\n<pre>phone_num_regex=re.compile(r\\'\\\\d\\\\d\\\\d\\\\d-\\\\d\\\\d\\\\d\\\\d\\\\d\\\\d\\\\d\\\\d\\')\n<\/pre>\n<\/div>\n<p>\uff083\uff09\u5339\u914d Regex \u5bf9\u8c61<br \/>\n\u63a5\u7740\uff0c \u628a\u9700\u8981\u67e5\u627e\u7684\u5b57\u7b26\u4e32\u4f20\u5165 Regex \u5bf9\u8c61\u7684 search() \u65b9\u6cd5\uff0c\u5bfb\u627e\u8be5\u6b63\u5219\u8868\u8fbe\u5f0f\u6240\u5b9a\u4e49\u7684\u6240\u6709\u5339\u914d\u3002\u5982\u679c\u5728\u8be5\u5b57\u7b26\u4e32\u4e2d\u6ca1\u6709\u627e\u5230\uff0c \u90a3\u4e48 search() \u65b9\u6cd5\u4f1a\u8fd4\u56de None\u3002 \u5982\u679c\u627e\u5230\uff0c search() \u65b9\u6cd5\u4f1a\u8fd4\u56de\u4e00\u4e2a Match \u5bf9\u8c61\u3002 Match \u5bf9\u8c61\u6709\u4e00\u4e2a group() \u65b9\u6cd5\uff0c\u901a\u8fc7\u5b83\u53ef\u4ee5\u8fd4\u56de\u88ab\u67e5\u627e\u5b57\u7b26\u4e32\u4e2d\u5b9e\u9645\u5339\u914d\u7684\u6587\u672c\u3002<\/p>\n<div>\n<pre>mo=phone_num_regex.search(\\'\u6211\u7684\u8054\u7cfb\u53f7\u7801\u4e3a\uff1a 0591-83822032.\\')\nprint(\\'\u67e5\u5230\u7684\u8054\u7cfb\u53f7\u7801: \\'+mo.group())\n<\/pre>\n<\/div>\n<p>\uff084\uff09\u4f20\u9012\u539f\u59cb\u5b57\u7b26\u4e32<br \/>\n\u8fd9\u91cc\u901a\u8fc7\u5728\u5b57\u7b26\u4e32\u7684\u7b2c\u4e00\u4e2a\u5f15\u53f7\u4e4b\u524d\u52a0\u4e0a r \uff0c\u5c06\u8be5\u5b57\u7b26\u4e32\u6807\u8bb0\u4e3a\u539f\u59cb\u5b57\u7b26\u4e32\u3002\u56e0\u4e3a\u5012\u659c\u6760\uff08 \\\\ \uff09 \u662f Python \u4e2d\u8f6c\u4e49\u5b57\u7b26\u5199\u6cd5\uff0c\u6211\u4eec\u5fc5\u987b\u4f7f\u7528 \\\\\\\\ \uff0c\u624d\u80fd\u6253\u5370\u51fa\u4e00\u4e2a\u5012\u659c\u6760\u3002\u6240\u4ee5\u4f7f\u7528 r \u8bed\u6cd5\uff0c\u4f1a\u8ba9\u6b63\u5219\u8868\u8fbe\u5f0f\u66f4\u7b80\u6d01\u3002<\/p>\n<hr \/>\n<p>\u603b\u7ed3\u5982\u4e0b\uff1a<\/p>\n<ol>\n<li>\u4f7f\u7528 import re \u5bfc\u5165\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5757 \u3002<\/li>\n<li>\u4f7f\u7528 re.compile() \u51fd\u6570\uff0c\u5e76\u4f7f\u7528\u539f\u59cb\u5b57\u7b26\u4e32\u8bed\u6cd5\uff0c\u521b\u5efa\u4e00\u4e2a Regex \u5bf9\u8c61 \u3002<\/li>\n<li>\u5411 Regex \u5bf9\u8c61\u7684 search() \u65b9\u6cd5\u4f20\u5165\u60f3\u67e5\u627e\u7684\u5b57\u7b26\u4e32\uff0c\u5f97\u5230\u4e00\u4e2a Match \u5bf9\u8c61 \u3002<\/li>\n<li>\u8c03\u7528 Match \u5bf9\u8c61\u7684 group() \u65b9\u6cd5\uff0c\u8fd4\u56de\u5b9e\u9645\u5339\u914d\u7684\u6587\u672c\u5b57\u7b26\u4e32\u3002<\/li>\n<\/ol>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u662f\u67e5\u627e\u6587\u672c\u7684\u4e00\u79cd\u63cf\u8ff0\u65b9\u6cd5\u3002\u4f8b\u5982\uff0c \u4e00\u4e2a\u6b63\u5219\u8868\u8fbe\u5f0f\u4e3a \\\\d\uff0c \u8868\u793a\u4e00\u4f4d\u6570\u5b57\u5b57\u7b26\uff0c\u5373\u4e00\u4f4d 0 \u5230 9 &hellip;<\/p>\n<p class=\"read-more\"> <a class=\"\" href=\"http:\/\/www.weijing.co\/?p=1751\"> <span class=\"screen-reader-text\">Python \u7684\u6b63\u5219\u8868\u8fbe\u5f0f<\/span> \u67e5\u770b\u5168\u6587 &raquo;<\/a><\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"default","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"default","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":""},"categories":[106,126],"tags":[],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false,"mailpoet_newsletter_max":false,"woocommerce_thumbnail":false,"woocommerce_single":false,"woocommerce_gallery_thumbnail":false,"shop_catalog":false,"shop_single":false,"shop_thumbnail":false},"uagb_author_info":{"display_name":"weijing","author_link":"http:\/\/www.weijing.co\/?author=4"},"uagb_comment_info":0,"uagb_excerpt":"\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u662f\u67e5\u627e\u6587\u672c\u7684\u4e00\u79cd\u63cf\u8ff0\u65b9\u6cd5\u3002\u4f8b\u5982\uff0c \u4e00\u4e2a\u6b63\u5219\u8868\u8fbe\u5f0f\u4e3a \\\\d\uff0c \u8868\u793a\u4e00\u4f4d\u6570\u5b57\u5b57\u7b26\uff0c\u5373\u4e00\u4f4d 0 \u5230 9&hellip;","_links":{"self":[{"href":"http:\/\/www.weijing.co\/index.php?rest_route=\/wp\/v2\/posts\/1751"}],"collection":[{"href":"http:\/\/www.weijing.co\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.weijing.co\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.weijing.co\/index.php?rest_route=\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"http:\/\/www.weijing.co\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1751"}],"version-history":[{"count":0,"href":"http:\/\/www.weijing.co\/index.php?rest_route=\/wp\/v2\/posts\/1751\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.weijing.co\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1751"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.weijing.co\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1751"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.weijing.co\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1751"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}