{"id":259,"date":"2021-08-13T21:58:45","date_gmt":"2021-08-13T13:58:45","guid":{"rendered":"http:\/\/101.34.19.194\/?p=259"},"modified":"2021-08-13T21:59:20","modified_gmt":"2021-08-13T13:59:20","slug":"python%e6%ad%a3%e5%88%99%e8%a1%a8%e8%be%be%e5%bc%8f","status":"publish","type":"post","link":"http:\/\/101.34.19.194\/?p=259","title":{"rendered":"Python\u6b63\u5219\u8868\u8fbe\u5f0f"},"content":{"rendered":"\n<p>\u5728Python\u4e2d\u9700\u8981\u901a\u8fc7\u6b63\u5219\u8868\u8fbe\u5f0f\u5bf9\u5b57\u7b26\u4e32\u8fdb\u2f8f\u5339\u914d\u7684\u65f6\u5019\uff0c\u53ef\u4ee5\u4f7f\u2f64\u2f00\u4e2apython\u81ea\u5e26\u7684\u6a21\u5757\uff0c\u540d\u5b57\u4e3are\u3002<\/p>\n\n\n\n<p>re\u6a21\u5757\u7684\u4f7f\u7528\uff1a<code>import re<\/code><\/p>\n\n\n\n<p>\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u5927\u81f4\u5339\u914d\u8fc7\u7a0b\u662f\uff1a<\/p>\n\n\n\n<p>1.\u4f9d\u6b21\u62ff\u51fa\u8868\u8fbe\u5f0f\u548c\u6587\u672c\u4e2d\u7684\u5b57\u7b26\u6bd4\u8f83\uff0c<\/p>\n\n\n\n<p>2.\u5982\u679c\u6bcf\u4e00\u4e2a\u5b57\u7b26\u90fd\u80fd\u5339\u914d\uff0c\u5219\u5339\u914d\u6210\u529f\uff1b\u4e00\u65e6\u6709\u5339\u914d\u4e0d\u6210\u529f\u7684\u5b57\u7b26\u5219\u5339\u914d\u5931\u8d25\u3002<\/p>\n\n\n\n<p>3.\u5982\u679c\u8868\u8fbe\u5f0f\u4e2d\u6709\u91cf\u8bcd\u6216\u8fb9\u754c\uff0c\u8fd9\u4e2a\u8fc7\u7a0b\u4f1a\u7a0d\u5fae\u6709\u4e00\u4e9b\u4e0d\u540c\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u5339\u914d\u5b57\u7b26<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>\u5b57\u7b26<\/th><th>\u529f\u80fd<\/th><\/tr><\/thead><tbody><tr><td>.<\/td><td>\u5339\u914d\u4efb\u610f1\u4e2a\u5b57\u7b26\uff08\u9664\u4e86\\n\uff09<\/td><\/tr><tr><td>\\d<\/td><td>\u5339\u914d\u4e00\u4e2a\u6570\u5b57\uff0c\u53730-9<\/td><\/tr><tr><td>\\D<\/td><td>\u5339\u914d\u975e\u6570\u5b57\uff0c\u5373\u4e0d\u662f\u6570\u5b57<\/td><\/tr><tr><td>\\s<\/td><td>\u5339\u914d\u7a7a\u767d\uff0c\u5373\u7a7a\u683c\uff0ctab\u952e<\/td><\/tr><tr><td>\\S<\/td><td>\u5339\u914d\u975e\u7a7a\u767d\u5b57\u7b26<\/td><\/tr><tr><td>\\w<\/td><td>\u5339\u914d\u4e00\u4e2a\u5b57\u6bcd\u6216\u6570\u5b57<\/td><\/tr><tr><td>\\W<\/td><td>\u5339\u914d\u975e\u5355\u8bcd\u6570\u5b57\u5b57\u7b26<\/td><\/tr><tr><td>*<\/td><td>\u8868\u793a\u4efb\u610f\u4e2a\u5b57\u7b26\uff08\u5305\u62ec0\u4e2a\uff09<\/td><\/tr><tr><td>+<\/td><td>\u8868\u793a\u81f3\u5c11\u4e00\u4e2a\u5b57\u7b26<\/td><\/tr><tr><td>?<\/td><td>\u8868\u793a0\u4e2a\u62161\u4e2a\u5b57\u7b26<\/td><\/tr><tr><td>{n}<\/td><td>\u8868\u793an\u4e2a\u5b57\u7b26<\/td><\/tr><tr><td>{n,m}<\/td><td>\u8868\u793an-m\u4e2a\u5b57\u7b26<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u4f8b\u5b50\u4e00<\/p>\n\n\n\n<p><code>\\d{3}\\s+\\d{3,8}<\/code><\/p>\n\n\n\n<p>1\u3001<code>\\d{3}<\/code>\u8868\u793a\u5339\u914d3\u4e2a\u6570\u5b57\uff0c\u4f8b\u5982<code>'010'<\/code>\uff1b<\/p>\n\n\n\n<p>2\u3001<code>\\s<\/code>\u53ef\u4ee5\u5339\u914d\u4e00\u4e2a\u7a7a\u683c\uff08\u4e5f\u5305\u62ecTab\u7b49\u7a7a\u767d\u7b26\uff09\uff0c\u6240\u4ee5<code>\\s+<\/code>\u8868\u793a\u81f3\u5c11\u6709\u4e00\u4e2a\u7a7a\u683c\uff0c\u4f8b\u5982\u5339\u914d<code>' '<\/code>\uff0c<code>' '<\/code>\u7b49\uff1b<\/p>\n\n\n\n<p>3\u3001<code>\\d{3,8}<\/code>\u8868\u793a3-8\u4e2a\u6570\u5b57\uff0c\u4f8b\u5982<code>'1234567'<\/code>\u3002<\/p>\n\n\n\n<p>\u4e0a\u9762\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u53ef\u4ee5\u5339\u914d\u4ee5\u4efb\u610f\u4e2a\u7a7a\u683c\u9694\u5f00\u7684\u5e26\u533a\u53f7\u7684\u7535\u8bdd\u53f7\u7801<\/p>\n\n\n\n<p>\u5982<code>010 12345<\/code><\/p>\n\n\n\n<p>\u5982\u679c\u8981\u5339\u914d<code>'010-12345'<\/code>\u8fd9\u6837\u7684\u53f7\u7801\uff0c\u7531\u4e8e<code>'-'<\/code>\u662f\u7279\u6b8a\u5b57\u7b26\uff0c\u5728\u6b63\u5219\u8868\u8fbe\u5f0f\u4e2d\uff0c\u8981\u7528<code>'\\'<\/code>\u8f6c\u4e49\uff0c\u6240\u4ee5\uff0c\u4e0a\u9762\u7684\u6b63\u5219\u662f<code>\\d{3}\\-\\d{3,8}<\/code>\u3002<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>\u5b57\u7b26<\/th><th>\u529f\u80fd<\/th><th>\u4f8b\u5b50<\/th><\/tr><\/thead><tbody><tr><td>[]<\/td><td>\u8868\u793a\u8303\u56f4<\/td><td><code>[0-9a-zA-Z\\_]<\/code>\u53ef\u4ee5\u5339\u914d\u4e00\u4e2a\u6570\u5b57\u3001\u5b57\u6bcd\u6216\u8005\u4e0b\u5212\u7ebf<\/td><\/tr><tr><td>A|B<\/td><td>\u53ef\u4ee5\u5339\u914dA\u6216B<\/td><td><code>(P|p)ython<\/code>\u53ef\u4ee5\u5339\u914d<code>'Python'<\/code>\u6216\u8005<code>'python'<\/code><\/td><\/tr><tr><td>^<\/td><td>\u884c\u7684\u5f00\u5934<\/td><td><code>^\\d<\/code>\u8868\u793a\u5fc5\u987b\u4ee5\u6570\u5b57\u5f00\u5934<\/td><\/tr><tr><td>$<\/td><td>\u884c\u7684\u7ed3\u675f<\/td><td><code>\\d$<\/code>\u8868\u793a\u5fc5\u987b\u4ee5\u6570\u5b57\u7ed3\u675f<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<ul><li><code>[0-9a-zA-Z\\_]<\/code>\u53ef\u4ee5\u5339\u914d\u4e00\u4e2a\u6570\u5b57\u3001\u5b57\u6bcd\u6216\u8005\u4e0b\u5212\u7ebf\uff1b<\/li><li><code>[0-9a-zA-Z\\_]+<\/code>\u53ef\u4ee5\u5339\u914d\u81f3\u5c11\u7531\u4e00\u4e2a\u6570\u5b57\u3001\u5b57\u6bcd\u6216\u8005\u4e0b\u5212\u7ebf\u7ec4\u6210\u7684\u5b57\u7b26\u4e32\uff0c\u6bd4\u5982<code>'a100'<\/code>\uff0c<code>'0_Z'<\/code>\uff0c<code>'Py3000'<\/code>\u7b49\u7b49\uff1b<\/li><li><code>[a-zA-Z\\_][0-9a-zA-Z\\_]*<\/code>\u53ef\u4ee5\u5339\u914d\u7531\u5b57\u6bcd\u6216\u4e0b\u5212\u7ebf\u5f00\u5934\uff0c\u540e\u63a5\u4efb\u610f\u4e2a\u7531\u4e00\u4e2a\u6570\u5b57\u3001\u5b57\u6bcd\u6216\u8005\u4e0b\u5212\u7ebf\u7ec4\u6210\u7684\u5b57\u7b26\u4e32\uff0c\u4e5f\u5c31\u662fPython\u5408\u6cd5\u7684\u53d8\u91cf\uff1b<\/li><li><code>[a-zA-Z\\_][0-9a-zA-Z\\_]{0, 19}<\/code>\u66f4\u7cbe\u786e\u5730\u9650\u5236\u4e86\u53d8\u91cf\u7684\u957f\u5ea6\u662f1-20\u4e2a\u5b57\u7b26\uff08\u524d\u97621\u4e2a\u5b57\u7b26+\u540e\u9762\u6700\u591a19\u4e2a\u5b57\u7b26\uff09\u3002<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">re\u6a21\u5757<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">r\u524d\u7f00<\/h3>\n\n\n\n<p>r\uff1a\u5728\u5e26\u6709 <code>'r'<\/code> \u524d\u7f00\u7684\u5b57\u7b26\u4e32\u5b57\u9762\u503c\u4e2d\uff0c\u53cd\u659c\u6760\u4e0d\u5fc5\u505a\u4efb\u4f55\u7279\u6b8a\u5904\u7406\u3002<\/p>\n\n\n\n<p>\u7531\u4e8ePython\u7684\u5b57\u7b26\u4e32\u672c\u8eab\u4e5f\u7528<code>\\<\/code>\u8f6c\u4e49\uff0c\u6240\u4ee5\u8981\u7279\u522b\u6ce8\u610f<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>s = 'ABC\\\\-001' # Python\u7684\u5b57\u7b26\u4e32<br># \u5bf9\u5e94\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u5b57\u7b26\u4e32\u53d8\u6210\uff1a<br># 'ABC\\-001'<\/code><\/pre>\n\n\n\n<p>\u4f7f\u7528r\u524d\u7f00\u540e\uff0c\u5c31\u4e0d\u7528\u8003\u8651\u8f6c\u4e49\u95ee\u9898<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>s = r'ABC\\-001' # Python\u7684\u5b57\u7b26\u4e32<br># \u5bf9\u5e94\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u5b57\u7b26\u4e32\u4e0d\u53d8\uff1a<br># 'ABC\\-001'<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>re.match\u51fd\u6570<\/strong><\/h3>\n\n\n\n<p>re.match(pattern, string, flags=0)<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>pattern<\/th><th>\u5339\u914d\u7684\u6b63\u5219\u8868\u8fbe\u5f0f<\/th><\/tr><\/thead><tbody><tr><td>string<\/td><td>\u8981\u5339\u914d\u7684\u5b57\u7b26\u4e32<\/td><\/tr><tr><td>flags<\/td><td>\u6807\u5fd7\u4f4d\uff0c\u7528\u4e8e\u63a7\u5236\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u5339\u914d\u65b9\u5f0f\uff0c\u5982\uff1a\u662f\u5426\u533a\u5206\u5927\u5c0f\u5199\uff0c\u591a\u884c\u5339\u914d\u7b49\u7b49\u3002 re.I \u5ffd\u7565\u5927\u5c0f\u5199 re.L \u8868\u793a\u7279\u6b8a\u5b57\u7b26\u96c6 \\w, \\W, \\b, \\B, \\s, \\S \u4f9d\u8d56\u4e8e\u5f53\u524d\u73af\u5883 re.M \u591a\u884c\u6a21\u5f0f re.S \u5373\u4e3a . \u5e76\u4e14\u5305\u62ec\u6362\u884c\u7b26\u5728\u5185\u7684\u4efb\u610f\u5b57\u7b26\uff08. \u4e0d\u5305\u62ec\u6362\u884c\u7b26\uff09 re.U \u8868\u793a\u7279\u6b8a\u5b57\u7b26\u96c6 \\w, \\W, \\b, \\B, \\d, \\D, \\s, \\S \u4f9d\u8d56\u4e8e Unicode \u5b57\u7b26\u5c5e\u6027\u6570\u636e\u5e93 re.X \u4e3a\u4e86\u589e\u52a0\u53ef\u8bfb\u6027\uff0c\u5ffd\u7565\u7a7a\u683c\u548c # \u540e\u9762\u7684\u6ce8\u91ca<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\">\u4f8b\u4e00<\/h4>\n\n\n\n<p><code>match()<\/code>\u65b9\u6cd5\u5224\u65ad\u662f\u5426\u5339\u914d\uff0c\u5982\u679c\u5339\u914d\u6210\u529f\uff0c\u8fd4\u56de\u4e00\u4e2a<code>Match<\/code>\u5bf9\u8c61\uff0c\u5426\u5219\u8fd4\u56de<code>None<\/code>\u3002\u5e38\u89c1\u7684\u5224\u65ad\u65b9\u6cd5\u5c31\u662f\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import re<br>text = '1234aa'<br>if re.match(r'1234AA',text,re.I):<br> &nbsp; &nbsp;print('ok')<br>else:<br> &nbsp; &nbsp;print('fail')<\/code><\/pre>\n\n\n\n<p>\u7531\u4e8e\u5ffd\u7565\u5927\u5c0f\u5199\uff08re.I)\uff0c\u6240\u4ee5\u8f93\u51fa<code>ok<\/code><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u4f8b\u4e8c<\/h4>\n\n\n\n<p>group()\u7528\u6765\u63d0\u51fa\u5206\u7ec4\u622a\u83b7\u7684\u5b57\u7b26\u4e32\uff0c\uff08\uff09\u7528\u6765\u5206\u7ec4\uff0cgroup() \u540cgroup\uff080\uff09\u5c31\u662f\u5339\u914d\u6b63\u5219\u8868\u8fbe\u5f0f\u6574\u4f53\u7ed3\u679c\uff0cgroup(1) \u5217\u51fa\u7b2c\u4e00\u4e2a\u62ec\u53f7\u5339\u914d\u90e8\u5206\uff0cgroup(2) \u5217\u51fa\u7b2c\u4e8c\u4e2a\u62ec\u53f7\u5339\u914d\u90e8\u5206\uff0cgroup(3) \u5217\u51fa\u7b2c\u4e09\u4e2a\u62ec\u53f7\u5339\u914d\u90e8\u5206\u3002\u6ca1\u6709\u5339\u914d\u6210\u529f\u7684\uff0cre.search()\u8fd4\u56deNone\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import re<br>ret = re.match(\"(&#91;^-]*)-(\\d+)\",\"010-12345678\")<br>print(ret.group())<br>print(ret.group(1))<br>print(ret.group(2))<\/code><\/pre>\n\n\n\n<p>\u8f93\u51fa<\/p>\n\n\n\n<p><code>010-12345678 010 12345678<\/code><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u4f8b\u4e09<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>import re<br>email_list = &#91;\"123@qq.com\", \"123@163.com\", \"654@cloud.com\",\"645@baidu.com\"]<br>for email in email_list:<br> &nbsp; &nbsp;ret = re.match(\"\\d{3,10}@(qq|163|cloud)\\.com$\", email)<br> &nbsp; &nbsp;if ret:<br> &nbsp; &nbsp; &nbsp; &nbsp;print(\"%s \u662f\u7b26\u5408\u89c4\u5b9a\u7684\u5730\u5740,\u5339\u914d\u540e\u7684\u7ed3\u679c\u662f:%s\" % (email, ret.group()))<br> &nbsp; &nbsp;else:<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;print(\"%s \u4e0d\u7b26\u5408\u8981\u6c42\" % email)<\/code><\/pre>\n\n\n\n<p>\u5339\u914d\u51faqq\u3001163\u3001cloud\u7684\u90ae\u7bb1<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u4f8b\u56db<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>import re<br>tels = &#91;\"13100001234\", \"18912344321\", \"10086\", \"18800007777\"]<br>for tel in tels:<br> &nbsp; &nbsp;ret = re.match(\"1\\d{9}&#91;0-35-68-9]\", tel)<br> &nbsp; &nbsp;if ret:<br> &nbsp; &nbsp; &nbsp; &nbsp;print(ret.group())<br> &nbsp; &nbsp;else:<br> &nbsp; &nbsp; &nbsp; &nbsp;print(\"%s \u4e0d\u662f\u60f3\u8981\u7684\u2f3f\u673a\u53f7\" % tel)<\/code><\/pre>\n\n\n\n<p>\u63d0\u53d6\u4e0d\u662f\u4ee54\u30017\u7ed3\u5c3e\u7684\u2f3f\u673a\u53f7\u7801(11\u4f4d)\uff0c\u5176\u4e2d<code>[0-35-68-9]<\/code>\u8868\u793a\u8303\u56f4\uff0c0~3\u30015~6\u7b49\uff0c\u4e0d\u7528\u5206\u9694\u7b26\u5206\u5f00<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u4f8b\u4e94<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>import re<br># \u6b63\u786e\u7684\u7406\u89e3\u601d\u8def\uff1a\u5982\u679c\u5728\u7b2c\u2f00\u5bf9&lt;&gt;\u4e2d\u662f\u4ec0\u4e48\uff0c\u6309\u7406\u8bf4\u5728\u540e\u2faf\u7684\u90a3\u5bf9&lt;&gt;\u4e2d\u5c31\u5e94\u8be5\u662f\u4ec0\u4e48\u3002\u901a\u8fc7\u5f15\u2f64\u5206\u7ec4\u4e2d\u5339\u914d\u5230\u7684\u6570\u636e\u5373\u53ef\uff0c\u4f46\u662f\u8981\u6ce8\u610f\u662f\u5143\u5b57\u7b26\u4e32\uff0c\u5373\u7c7b\u4f3c r\"\"\u8fd9\u79cd\u683c\u5f0f\u3002<br>ret = re.match(r\"&lt;(&#91;a-zA-Z]*)&gt;\\w*&lt;\/\\1&gt;\", \"&lt;html&gt;hh&lt;\/html&gt;\")<br># \u56e0\u4e3a2\u5bf9&lt;&gt;\u4e2d\u7684\u6570\u636e\u4e0d\u2f00\u81f4\uff0c\u6240\u4ee5\u6ca1\u6709\u5339\u914d\u51fa\u6765<br>test_label = &#91;\"&lt;html&gt;hh&lt;\/html&gt;\",\"&lt;html&gt;hh&lt;\/htmlbalabala&gt;\"]<br>for label in test_label:<br> &nbsp; &nbsp;ret = re.match(r\"&lt;(&#91;a-zA-Z]*)&gt;\\w*&lt;\/\\1&gt;\", label)<br> &nbsp; &nbsp;if ret:<br> &nbsp; &nbsp; &nbsp; &nbsp;print(\"%s \u8fd9\u662f\u4e00\u5bf9\u6b63\u786e\u7684\u6807\u7b7e\" % ret.group())<br> &nbsp; &nbsp;else:<br> &nbsp; &nbsp; &nbsp; &nbsp;print(\"%s \u8fd9\u662f\u2f00\u5bf9\u4e0d\u6b63\u786e\u7684\u6807\u7b7e\" % label)<\/code><\/pre>\n\n\n\n<p>\\1,&#8230;,\\9\uff0c\u5339\u914d\u7b2cn\u4e2a\u5206\u7ec4\u7684\u5185\u5bb9\u3002\u5982\u4f8b\u5b50\u6240\u793a\uff0c\u6307\u5339\u914d\u7b2c\u4e00\u4e2a\u5206\u7ec4\u7684\u5185\u5bb9\u3002<\/p>\n\n\n\n<blockquote class=\"wp-block-quote\"><p>&lt;html&gt;hh&lt;\/html&gt; \u8fd9\u662f\u4e00\u5bf9\u6b63\u786e\u7684\u6807\u7b7e &lt;html&gt;hh&lt;\/htmlbalabala&gt; \u8fd9\u662f\u2f00\u5bf9\u4e0d\u6b63\u786e\u7684\u6807\u7b7e<\/p><\/blockquote>\n\n\n\n<h3 class=\"wp-block-heading\">re.split\u51fd\u6570<\/h3>\n\n\n\n<p>\u6839\u636e\u5339\u914d\u8fdb\u2f8f\u5207\u5272\u5b57\u7b26\u4e32\uff0c\u5e76\u8fd4\u56de\u2f00\u4e2a\u5217\u8868\u3002<\/p>\n\n\n\n<p><code>re.``split<\/code>(<em>pattern<\/em>, <em>string<\/em>, <em>maxsplit=0<\/em>, <em>flags=0<\/em>)<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>\u53c2\u6570<\/th><th>\u63cf\u8ff0<\/th><\/tr><\/thead><tbody><tr><td>pattern<\/td><td>\u5339\u914d\u7684\u6b63\u5219\u8868\u8fbe\u5f0f<\/td><\/tr><tr><td>string<\/td><td>\u8981\u5339\u914d\u7684\u5b57\u7b26\u4e32<\/td><\/tr><tr><td>maxsplit<\/td><td>\u5206\u9694\u6b21\u6570\uff0cmaxsplit=1 \u5206\u9694\u4e00\u6b21\uff0c\u9ed8\u8ba4\u4e3a 0\uff0c\u4e0d\u9650\u5236\u6b21\u6570<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u4f8b\u5b50<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import re<br>ret = re.split(r\":| \",\"info:xiaoZhang 33 shandong\")<br>print(ret)<\/code><\/pre>\n\n\n\n<p>\u8f93\u51fa<\/p>\n\n\n\n<p><code>['info', 'xiaoZhang', '33', 'shandong']<\/code><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">re.findall\u51fd\u6570<\/h3>\n\n\n\n<p>\u5728\u5b57\u7b26\u4e32\u4e2d\u627e\u5230\u6b63\u5219\u8868\u8fbe\u5f0f\u6240\u5339\u914d\u7684\u6240\u6709\u5b50\u4e32\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u5217\u8868\uff0c\u5982\u679c\u6ca1\u6709\u627e\u5230\u5339\u914d\u7684\uff0c\u5219\u8fd4\u56de\u7a7a\u5217\u8868\u3002\u6ce8\u610f<strong>\uff1a<\/strong> match \u548c search \u662f\u5339\u914d\u4e00\u6b21 findall \u5339\u914d\u6240\u6709\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import re\nret = re.findall(r\"\\d+\", \"python = 9999, c = 7890, c++ = 12345\")\nprint(ret)<\/code><\/pre>\n\n\n\n<p>\u8f93\u51fa<\/p>\n\n\n\n<p><code>['9999', '7890', '12345']<\/code><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">match\u4e0efindall<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>match\uff1a<\/strong><\/h4>\n\n\n\n<p><strong>\u4ece\u5f00\u5934\u4f4d\u7f6e\u5339\u914d,\u53ea\u5339\u914d\u4e00\u6b21\uff0c\u5f00\u5934\u5339\u914d\u4e0d\u4e0a\uff0c\u5219\u4e0d\u7ee7\u7eed\u5339\u914d a\uff0cb\uff0c\\w+<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>match(a,\"abcdef\") \u5339\u914da\n\n\\&gt;&gt;&gt; re.match(\"a\",\"abcdef\").group()\n\n'a'\n\nmatch\uff08b,\"abcdef\"\uff09\n\n\\&gt;&gt;&gt; print re.match(\"b\",\"abcdef\")\n\nNone\n\nmatch(\"\\w+\",\"abcdef\")<\/code><\/pre>\n\n\n\n<p><strong>findall<\/strong><\/p>\n\n\n\n<p><strong>\u6ce8\uff1afindall \u8fd4\u56de\u5217\u8868 \uff0c\u5217\u8868\u4e0d\u80fdgroup\uff08\uff09<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\\&gt;&gt;&gt; print re.findall(r\"b\",\"abcdef abc 123 456\")\n\n&#91;'b', 'b']\n\n\n\\&gt;&gt;&gt; print re.findall(r\"a\",\"abcdef abc 123 456\")\n\n&#91;'a', 'a']\n \n\\&gt;&gt;&gt; print re.findall(r\"\\w+\",\"abcdef abc 123 456\")\n\n&#91;'abcdef', 'abc', '123', '456']<\/code><\/pre>\n\n\n\n<p>\u8f93\u51fa\u5e26\u6709[0]\u5c31\u53ef\u4ee5\u8f93\u51fastr\u7c7b\u578b\uff0c\u5982<code>print result[0]<\/code>\u8f93\u51fa\u7b2c\u4e00\u4e2a\u5339\u914d\u5230\u7684str\u7c7b\u578b<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">python\u8d2a\u5a6a\u548c\u2fae\u8d2a\u5a6a<\/h2>\n\n\n\n<p>Python\u2fa5\u6570\u91cf\u8bcd\u9ed8\u8ba4\u662f\u8d2a\u5a6a\u7684\uff08\u5728\u5c11\u6570\u8bed\u2f94\u2fa5\u4e5f\u53ef\u80fd\u662f\u9ed8\u8ba4\u2fae\u8d2a\u5a6a\uff09\uff0c\u603b\u662f\u5c1d\u8bd5\u5339\u914d\u5c3d\u53ef\u80fd\u591a\u7684\u5b57\u7b26\uff1b\u2fae\u8d2a\u5a6a\u5219\u76f8\u53cd\uff0c\u603b\u662f\u5c1d\u8bd5\u5339\u914d\u5c3d\u53ef\u80fd\u5c11\u7684\u5b57\u7b26\u3002<\/p>\n\n\n\n<p>\u4f8b\u5982\uff1a\u6b63\u5219\u8868\u8fbe\u5f0f\u201dab<em>\u201d\u5982\u679c\u7528\u4e8e\u67e5\u627e\u201dabbbc\u201d\uff0c\u5c06\u627e\u5230\u201dabbb\u201d\u3002\u800c\u5982\u679c\u4f7f\u7528\u975e\u8d2a\u5a6a\u7684\u6570\u91cf\u8bcd\u201dab<\/em>?\u201d\uff0c\u5c06\u627e\u5230\u201da\u201d\u3002<\/p>\n\n\n\n<p>\u6ce8\uff1a\u6211\u4eec\u4e00\u822c\u4f7f\u7528\u975e\u8d2a\u5a6a\u6a21\u5f0f\u6765\u63d0\u53d6\u3002<\/p>\n\n\n\n<p>\u5728&#8221;*&#8221;,&#8221;?&#8221;,&#8221;+&#8221;,&#8221;{m,n}&#8221;\u540e\u2faf\u52a0\u4e0a\uff1f\uff0c\u4f7f\u8d2a\u5a6a\u53d8\u6210\u2fae\u8d2a\u5a6a\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u4f8b\u4e00<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>import re\ns=\"This is a number 234-235-22-423\"\n#\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f\u4e2d\u4f7f\u2f64\u5230\u901a\u914d\u5b57\uff0c\u90a3\u5b83\u5728\u4ece\u5de6\u5230\u53f3\u7684\u987a\u5e8f\u6c42\u503c\u65f6\uff0c\u4f1a\u5c3d\u91cf\u201c\u6293\u53d6\u201d\u6ee1\u2f9c\u5339\u914d\u6700\u2ed3\u5b57\u7b26\u4e32\uff0c\u5728\u6211\u4eec\u4e0a\u2faf\u7684\u4f8b\u2f26\u2fa5\u2faf\uff0c\u201c.+\u201d\u4f1a\u4ece\u5b57\u7b26\u4e32\u7684\u542f\u59cb\u5904\u6293\u53d6\u6ee1\u2f9c\u6a21\u5f0f\u7684\u6700\u2ed3\u5b57\u7b26\uff0c\u5176\u4e2d\u5305\u62ec\u6211\u4eec\u60f3\u5f97\u5230\u7684\u7b2c\u2f00\u4e2a\u6574\u578b\u5b57\u6bb5\u7684\u4e2d\u7684\u2f24\u90e8\u5206\uff0c\u201c\\d+\u201d\u53ea\u9700\u2f00\u4f4d\u5b57\u7b26\u5c31\u53ef\u4ee5\u5339\u914d\uff0c\u6240\u4ee5\u5b83\u5339\u914d\u4e86\u6570\u5b57\u201c4\u201d\uff0c\u2f7d\u201c.+\u201d\u5219\u5339\u914d\u4e86\u4ece\u5b57\u7b26\u4e32\u8d77\u59cb\u5230\u8fd9\u4e2a\u7b2c\u2f00\u4f4d\u6570\u5b574\u4e4b\u524d\u7684\u6240\u6709\u5b57\u7b26\nr=re.match(\".+(\\d+-\\d+-\\d+-\\d+)\",s)\nprint(r.group(1))\n#\u2fae\u8d2a\u5a6a\u64cd\u4f5c\u7b26\u201c\uff1f\u201d\uff0c\u8fd9\u4e2a\u64cd\u4f5c\u7b26\u53ef\u4ee5\u2f64\u5728\"*\",\"+\",\"?\"\u7684\u540e\u2faf\uff0c\u8981\u6c42\u6b63\u5219\u5339\u914d\u7684\u8d8a\u5c11\u8d8a\u597d\nr=re.match(\".+?(\\d+-\\d+-\\d+-\\d+)\",s)\nprint(r.group(1))<\/code><\/pre>\n\n\n\n<p>\u7ed3\u679c\uff1a<\/p>\n\n\n\n<p>4-235-22-423 234-235-22-423<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u4f8b\u4e8c<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>import re\ntest_str=\"&lt;img data-original=https:\/\/rpic.douyucdn.cn\/appCovers\/2016\/11\/13\/1213973.jpg&gt;\"\nret = re.search(r\"https:\/\/.*?.jpg\", test_str)\nprint(ret.group())<\/code><\/pre>\n\n\n\n<p>\u7ed3\u679c\uff1a<a href=\"https:\/\/rpic.douyucdn.cn\/appCovers\/2016\/11\/13\/1213973.jpg\">https:\/\/rpic.douyucdn.cn\/appCovers\/2016\/11\/13\/1213973.jpg<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u4f8b\u9898<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u4f8b\u4e00<\/h3>\n\n\n\n<p>\u4e2d\u79d1\u59272017\u4fe1\u606f\u5b89\u5168\u5927\u8d5b \u771f\u5047flag<\/p>\n\n\n\n<p>\u6253\u5f00\u4e00\u6bb5\u4e71\u7684flag\uff0c\u771f\u7684flag\u85cf\u5728\u5176\u4e2d<\/p>\n\n\n\n<figure class=\"wp-block-image\"><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/gitee.com\/hermitaria\/blogimagee\/raw\/master\/20210808220213.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  decoding=\"async\" data-original=\"https:\/\/gitee.com\/hermitaria\/blogimagee\/raw\/master\/20210808220213.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"image-20210808220206744\"\/><\/div><\/figure>\n\n\n\n<p>\u7528python\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u53bb\u5339\u914d<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import re\nf = open(\"flag.txt\",\"rb\")\ntxts = f.readlines()\nf.close()\nfor txt in txts:\n    txt = str(txts)\n    flag = re.findall(\"flag\\{\\w+}\",txt)\n    print(flag&#91;0])<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u4f8b\u4e8c<\/h3>\n\n\n\n<p>bugku \u79cb\u540d\u5c71\u8f66\u795e<\/p>\n\n\n\n<figure class=\"wp-block-image\"><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/img-blog.csdnimg.cn\/2021011100042066.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  decoding=\"async\" data-original=\"https:\/\/img-blog.csdnimg.cn\/2021011100042066.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"img\"\/><\/div><\/figure>\n\n\n\n<p>2s\u5237\u65b0\u4e00\u6b21\uff0c\u67e5\u770b\u6e90\u7801<\/p>\n\n\n\n<figure class=\"wp-block-image\"><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/img-blog.csdnimg.cn\/20210111000403832.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  decoding=\"async\" data-original=\"https:\/\/img-blog.csdnimg.cn\/20210111000403832.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"img\"\/><\/div><\/figure>\n\n\n\n<p>\u5199python\u811a\u672c\u83b7\u53d6\u9875\u9762\u6570\u636e\u5e76\u8ba1\u7b97post\u63d0\u4ea4\u7ed3\u679c\u83b7\u5f97flag<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import re\nimport requests\n\nsess = requests.session()\nresponse = sess.get(\"http:\/\/114.67.246.176:17986\/\").text\ncalc = eval(re.findall(r\"(\\d+&#91;+\\-*])+(\\d+)\",response)&#91;0])\nkey = {\n    'value':calc\n}\nflag = sess.post(\"http:\/\/114.67.246.176:17986\/\",data=key)\nprint(flag.text)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u4f8b\u4e09<\/h3>\n\n\n\n<p>[\u5f3a\u7f51\u676f 2019]\u9ad8\u660e\u7684\u9ed1\u5ba2<\/p>\n\n\n\n<p>\u4e0b\u8f7d\u4e4b\u540e\u662f\u51e0\u5343\u4e2aphp\u6587\u4ef6\uff0c\u91cc\u9762\u6709\u5404\u79cdshell<\/p>\n\n\n\n<p>\u4e0d\u8fc7\u5f88\u591ashell\u90fd\u6ca1\u7528\uff0c\u4f46\u80af\u5b9a\u5b58\u5728\u51e0\u4e2ashell\u53ef\u4ee5\u7528\uff0c\u53ea\u80fd\u8dd1\u811a\u672c\u53bb\u5c1d\u8bd5<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import os\nimport requests\nimport re\nimport threading\nimport time\nprint('\u5f00\u59cb\u65f6\u95f4\uff1a  '+  time.asctime( time.localtime(time.time()) ))\ns1=threading.Semaphore(100)  \t\t\t\t\t\t\t  \t\t\t#\u8fd9\u513f\u8bbe\u7f6e\u6700\u5927\u7684\u7ebf\u7a0b\u6570\nfilePath = r\"D:\/soft\/phpstudy\/PHPTutorial\/WWW\/src\/\"\nos.chdir(filePath)\t\t\t\t\t\t\t\t\t\t\t\t\t#\u6539\u53d8\u5f53\u524d\u7684\u8def\u5f84\nrequests.adapters.DEFAULT_RETRIES = 5\t\t\t\t\t\t\t\t#\u8bbe\u7f6e\u91cd\u8fde\u6b21\u6570\uff0c\u9632\u6b62\u7ebf\u7a0b\u6570\u8fc7\u9ad8\uff0c\u65ad\u5f00\u8fde\u63a5\nfiles = os.listdir(filePath)\nsession = requests.Session()\nsession.keep_alive = False\t\t\t\t\t\t\t\t\t\t\t # \u8bbe\u7f6e\u8fde\u63a5\u6d3b\u8dc3\u72b6\u6001\u4e3aFalse\ndef get_content(file):\n    s1.acquire()\t\t\t\t\t\t\t\t\t\t\t\t\n    print('trying   '+file+ '     '+ time.asctime( time.localtime(time.time()) ))\n    with open(file,encoding='utf-8') as f:\t\t\t\t\t\t\t#\u6253\u5f00php\u6587\u4ef6\uff0c\u63d0\u53d6\u6240\u6709\u7684$_GET\u548c$_POST\u7684\u53c2\u6570\n            gets = list(re.findall('\\$_GET\\&#91;\\'(.*?)\\'\\]', f.read()))\n            posts = list(re.findall('\\$_POST\\&#91;\\'(.*?)\\'\\]', f.read()))\n    data = {}\t\t\t\t\t\t\t\t\t\t\t\t\t\t#\u6240\u6709\u7684$_POST\n    params = {}\t\t\t\t\t\t\t\t\t\t\t\t\t\t#\u6240\u6709\u7684$_GET\n    for m in gets:\n        params&#91;m] = \"echo 'xxxxxx';\"\n    for n in posts:\n        data&#91;n] = \"echo 'xxxxxx';\"\n    url = 'http:\/\/127.0.0.1\/src\/'+file\n    req = session.post(url, data=data, params=params)\t\t\t#\u4e00\u6b21\u6027\u8bf7\u6c42\u6240\u6709\u7684GET\u548cPOST\n    req.close()\t\t\t\t\t\t\t\t\t\t\t\t# \u5173\u95ed\u8bf7\u6c42  \u91ca\u653e\u5185\u5b58\n    req.encoding = 'utf-8'\n    content = req.text\n    #print(content)\n    if \"xxxxxx\" in content:\t\t\t\t\t\t\t\t\t#\u5982\u679c\u53d1\u73b0\u6709\u53ef\u4ee5\u5229\u7528\u7684\u53c2\u6570\uff0c\u7ee7\u7eed\u7b5b\u9009\u51fa\u5177\u4f53\u7684\u53c2\u6570\n        flag = 0\n        for a in gets:\n            req = session.get(url+'?%s='%a+\"echo 'xxxxxx';\")\n            content = req.text\n            req.close()\t\t\t\t\t\t\t\t\t\t\t\t# \u5173\u95ed\u8bf7\u6c42  \u91ca\u653e\u5185\u5b58\n            if \"xxxxxx\" in content:\n                flag = 1\n                break\n        if flag != 1:\n            for b in posts:\n                req = session.post(url, data={b:\"echo 'xxxxxx';\"})\n                content = req.text\n                req.close()\t\t\t\t\t\t\t\t\t\t\t\t# \u5173\u95ed\u8bf7\u6c42  \u91ca\u653e\u5185\u5b58\n                if \"xxxxxx\" in content:\n                    break\n        if flag == 1:\t\t\t\t\t\t\t\t\t\t\t\t\t#flag\u7528\u6765\u5224\u65ad\u53c2\u6570\u662fGET\u8fd8\u662fPOST\uff0c\u5982\u679c\u662fGET\uff0cflag==1\uff0c\u5219b\u672a\u5b9a\u4e49\uff1b\u5982\u679c\u662fPOST\uff0cflag\u4e3a0\uff0c\n            param = a\n        else:\n            param = b\n        print('\u627e\u5230\u4e86\u5229\u7528\u6587\u4ef6\uff1a '+file+\"  and \u627e\u5230\u4e86\u5229\u7528\u7684\u53c2\u6570\uff1a%s\" %param)\n        print('\u7ed3\u675f\u65f6\u95f4\uff1a  ' + time.asctime(time.localtime(time.time())))\n    s1.release()\n\nfor i in files:\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t#\u52a0\u5165\u591a\u7ebf\u7a0b\n   t = threading.Thread(target=get_content, args=(i,))\n   t.start()\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5728Python\u4e2d\u9700\u8981\u901a\u8fc7\u6b63\u5219\u8868\u8fbe\u5f0f\u5bf9\u5b57\u7b26\u4e32\u8fdb\u2f8f\u5339\u914d\u7684\u65f6\u5019\uff0c\u53ef\u4ee5\u4f7f\u2f64\u2f00\u4e2apython\u81ea\u5e26\u7684\u6a21\u5757\uff0c\u540d\u5b57\u4e3are\u3002 r [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[3],"tags":[21],"_links":{"self":[{"href":"http:\/\/101.34.19.194\/index.php?rest_route=\/wp\/v2\/posts\/259"}],"collection":[{"href":"http:\/\/101.34.19.194\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/101.34.19.194\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/101.34.19.194\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/101.34.19.194\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=259"}],"version-history":[{"count":2,"href":"http:\/\/101.34.19.194\/index.php?rest_route=\/wp\/v2\/posts\/259\/revisions"}],"predecessor-version":[{"id":261,"href":"http:\/\/101.34.19.194\/index.php?rest_route=\/wp\/v2\/posts\/259\/revisions\/261"}],"wp:attachment":[{"href":"http:\/\/101.34.19.194\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=259"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/101.34.19.194\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=259"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/101.34.19.194\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=259"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}