{"id":1145060,"date":"2025-01-08T23:05:04","date_gmt":"2025-01-08T15:05:04","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1145060.html"},"modified":"2025-01-08T23:05:08","modified_gmt":"2025-01-08T15:05:08","slug":"python%e4%b8%ad%e5%8c%b9%e9%85%8d%e6%96%87%e4%bb%b6%e5%90%8d%e5%a6%82%e4%bd%95%e7%94%a8%e6%ad%a3%e5%88%99%e8%a1%a8%e8%be%be%e5%bc%8f","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1145060.html","title":{"rendered":"python\u4e2d\u5339\u914d\u6587\u4ef6\u540d\u5982\u4f55\u7528\u6b63\u5219\u8868\u8fbe\u5f0f"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24181832\/15b71bf6-cfbf-4454-81b8-073f2c16fbb7.webp\" alt=\"python\u4e2d\u5339\u914d\u6587\u4ef6\u540d\u5982\u4f55\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u5339\u914d\u6587\u4ef6\u540d\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\uff08Regular Expressions, \u7b80\u79f0regex\uff09\u6765\u5b9e\u73b0\u3002\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5728\u5b57\u7b26\u4e32\u4e2d\u641c\u7d22\u3001\u5339\u914d\u548c\u66ff\u6362\u7279\u5b9a\u7684\u6a21\u5f0f\u3002Python \u63d0\u4f9b\u4e86\u4e00\u4e2a\u5185\u7f6e\u7684\u6a21\u5757 <code>re<\/code> \u6765\u5904\u7406\u6b63\u5219\u8868\u8fbe\u5f0f\u3002\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u6587\u4ef6\u540d\u65f6\uff0c\u6838\u5fc3\u6b65\u9aa4\u5305\u62ec\uff1a\u5b9a\u4e49\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f\u3001\u7f16\u8bd1\u6b63\u5219\u8868\u8fbe\u5f0f\u3001\u4f7f\u7528\u5339\u914d\u51fd\u6570\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u6b65\u9aa4\u5e76\u7ed9\u51fa\u5177\u4f53\u793a\u4f8b\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b9a\u4e49\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f<\/h3>\n<\/p>\n<p><p>\u5728\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u6587\u4ef6\u540d\u4e4b\u524d\uff0c\u9996\u5148\u9700\u8981\u5b9a\u4e49\u4e00\u4e2a\u5408\u9002\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f\u3002\u8fd9\u4e2a\u6a21\u5f0f\u5c06\u63cf\u8ff0\u4f60\u5e0c\u671b\u5339\u914d\u7684\u6587\u4ef6\u540d\u683c\u5f0f\u3002\u5e38\u89c1\u7684\u6587\u4ef6\u540d\u5339\u914d\u6a21\u5f0f\u5305\u62ec\u7279\u5b9a\u7684\u524d\u7f00\u3001\u540e\u7f00\u4ee5\u53ca\u6587\u4ef6\u6269\u5c55\u540d\u7b49\u3002<\/p>\n<\/p>\n<p><p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>pattern = r&#39;^[a-zA-Z0-9_\\-]+\\.txt$&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u6a21\u5f0f\u63cf\u8ff0\u4e86\u4e00\u4e2a\u6587\u4ef6\u540d\uff0c\u5b83\u53ef\u4ee5\u5305\u542b\u5b57\u6bcd\u3001\u6570\u5b57\u3001\u4e0b\u5212\u7ebf\u6216\u8fde\u5b57\u7b26\uff0c\u5e76\u4e14\u4ee5 <code>.txt<\/code> \u7ed3\u5c3e\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u7f16\u8bd1\u6b63\u5219\u8868\u8fbe\u5f0f<\/h3>\n<\/p>\n<p><p>\u5728\u5b9a\u4e49\u4e86\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f\u4e4b\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>re.compile()<\/code> \u51fd\u6570\u5c06\u5176\u7f16\u8bd1\u6210\u4e00\u4e2a\u6b63\u5219\u8868\u8fbe\u5f0f\u5bf9\u8c61\u3002\u8fd9\u6837\u53ef\u4ee5\u63d0\u9ad8\u5339\u914d\u6548\u7387\uff0c\u5c24\u5176\u662f\u5728\u9700\u8981\u591a\u6b21\u5339\u914d\u7684\u60c5\u51b5\u4e0b\u3002<\/p>\n<\/p>\n<p><p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">regex = re.compile(pattern)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u5339\u914d\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u7f16\u8bd1\u540e\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u5bf9\u8c61\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u5339\u914d\u51fd\u6570\u6765\u5339\u914d\u5b57\u7b26\u4e32\uff0c\u5305\u62ec <code>match()<\/code>\u3001<code>search()<\/code> \u548c <code>findall()<\/code> \u7b49\u3002\u6bcf\u4e2a\u51fd\u6570\u7684\u7528\u9014\u4e0d\u540c\uff0c\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5b83\u4eec\u7684\u533a\u522b\u548c\u7528\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u8be6\u7ec6\u793a\u4f8b\u548c\u5e94\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><h4>1\u3001\u5339\u914d\u7279\u5b9a\u6269\u5c55\u540d\u7684\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u5047\u8bbe\u4f60\u9700\u8981\u5339\u914d\u6240\u6709 <code>.txt<\/code> \u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>pattern = r&#39;^[a-zA-Z0-9_\\-]+\\.txt$&#39;<\/p>\n<p>regex = re.compile(pattern)<\/p>\n<p>filenames = [&#39;file1.txt&#39;, &#39;file2.doc&#39;, &#39;file3.txt&#39;, &#39;file_4.txt&#39;, &#39;file-5.txt&#39;, &#39;file6.pdf&#39;]<\/p>\n<p>matched_files = [fname for fname in filenames if regex.match(fname)]<\/p>\n<p>print(matched_files)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8f93\u51fa\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code>[&#39;file1.txt&#39;, &#39;file3.txt&#39;, &#39;file_4.txt&#39;, &#39;file-5.txt&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u5339\u914d\u5177\u6709\u7279\u5b9a\u524d\u7f00\u7684\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u5047\u8bbe\u4f60\u9700\u8981\u5339\u914d\u6240\u6709\u4ee5 <code>log_<\/code> \u5f00\u5934\u7684\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>pattern = r&#39;^log_[a-zA-Z0-9_\\-]+\\.log$&#39;<\/p>\n<p>regex = re.compile(pattern)<\/p>\n<p>filenames = [&#39;log_20210101.log&#39;, &#39;log_20210102.log&#39;, &#39;data_20210101.log&#39;, &#39;log20210103.log&#39;]<\/p>\n<p>matched_files = [fname for fname in filenames if regex.match(fname)]<\/p>\n<p>print(matched_files)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8f93\u51fa\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code>[&#39;log_20210101.log&#39;, &#39;log_20210102.log&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u5339\u914d\u65e5\u671f\u683c\u5f0f\u7684\u6587\u4ef6\u540d<\/h4>\n<\/p>\n<p><p>\u5047\u8bbe\u4f60\u9700\u8981\u5339\u914d\u6240\u6709\u5305\u542b\u65e5\u671f\uff08\u683c\u5f0f\u5982 <code>YYYYMMDD<\/code>\uff09\u7684\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>pattern = r&#39;^[a-zA-Z0-9_\\-]+_[0-9]{8}\\.log$&#39;<\/p>\n<p>regex = re.compile(pattern)<\/p>\n<p>filenames = [&#39;log_20210101.log&#39;, &#39;log_20210102.log&#39;, &#39;log_2021_01_01.log&#39;, &#39;log20210103.log&#39;]<\/p>\n<p>matched_files = [fname for fname in filenames if regex.match(fname)]<\/p>\n<p>print(matched_files)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8f93\u51fa\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code>[&#39;log_20210101.log&#39;, &#39;log_20210102.log&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u4e2d\u7684\u7279\u6b8a\u5b57\u7b26<\/h3>\n<\/p>\n<p><p>\u5728\u5b9a\u4e49\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f\u65f6\uff0c\u9700\u8981\u6ce8\u610f\u4e00\u4e9b\u7279\u6b8a\u5b57\u7b26\u53ca\u5176\u542b\u4e49\uff1a<\/p>\n<\/p>\n<ul>\n<li><code>^<\/code>\uff1a\u5339\u914d\u5b57\u7b26\u4e32\u7684\u5f00\u59cb\u3002<\/li>\n<li><code>$<\/code>\uff1a\u5339\u914d\u5b57\u7b26\u4e32\u7684\u7ed3\u5c3e\u3002<\/li>\n<li><code>.<\/code>\uff1a\u5339\u914d\u4efb\u610f\u5355\u4e2a\u5b57\u7b26\u3002<\/li>\n<li><code>*<\/code>\uff1a\u5339\u914d\u524d\u9762\u7684\u5b57\u7b26\u96f6\u6b21\u6216\u591a\u6b21\u3002<\/li>\n<li><code>+<\/code>\uff1a\u5339\u914d\u524d\u9762\u7684\u5b57\u7b26\u4e00\u6b21\u6216\u591a\u6b21\u3002<\/li>\n<li><code>?<\/code>\uff1a\u5339\u914d\u524d\u9762\u7684\u5b57\u7b26\u96f6\u6b21\u6216\u4e00\u6b21\u3002<\/li>\n<li><code>[]<\/code>\uff1a\u5339\u914d\u62ec\u53f7\u5185\u7684\u4efb\u610f\u4e00\u4e2a\u5b57\u7b26\u3002<\/li>\n<li><code>{}<\/code>\uff1a\u5339\u914d\u524d\u9762\u7684\u5b57\u7b26\u6307\u5b9a\u6b21\u6570\u3002<\/li>\n<\/ul>\n<p><h3>\u516d\u3001\u590d\u6742\u6587\u4ef6\u540d\u5339\u914d<\/h3>\n<\/p>\n<p><h4>1\u3001\u5339\u914d\u5e26\u6709\u591a\u4e2a\u6269\u5c55\u540d\u7684\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u6709\u65f6\u6587\u4ef6\u540d\u53ef\u80fd\u5305\u542b\u591a\u4e2a\u6269\u5c55\u540d\uff0c\u4f8b\u5982 <code>.tar.gz<\/code>\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u6a21\u5f0f\u5339\u914d\uff1a<\/p>\n<\/p>\n<p><p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>pattern = r&#39;^[a-zA-Z0-9_\\-]+\\.(tar\\.gz|zip|rar)$&#39;<\/p>\n<p>regex = re.compile(pattern)<\/p>\n<p>filenames = [&#39;archive.tar.gz&#39;, &#39;document.zip&#39;, &#39;image.rar&#39;, &#39;file.txt&#39;]<\/p>\n<p>matched_files = [fname for fname in filenames if regex.match(fname)]<\/p>\n<p>print(matched_files)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8f93\u51fa\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code>[&#39;archive.tar.gz&#39;, &#39;document.zip&#39;, &#39;image.rar&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u5339\u914d\u5305\u542b\u6570\u5b57\u548c\u5b57\u6bcd\u7684\u6587\u4ef6\u540d<\/h4>\n<\/p>\n<p><p>\u5047\u8bbe\u4f60\u9700\u8981\u5339\u914d\u6587\u4ef6\u540d\u4e2d\u5305\u542b\u6570\u5b57\u548c\u5b57\u6bcd\u7684\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>pattern = r&#39;^[a-zA-Z0-9]+_[a-zA-Z0-9]+\\.txt$&#39;<\/p>\n<p>regex = re.compile(pattern)<\/p>\n<p>filenames = [&#39;file_123.txt&#39;, &#39;abc_456.txt&#39;, &#39;123_abc.txt&#39;, &#39;file123.txt&#39;]<\/p>\n<p>matched_files = [fname for fname in filenames if regex.match(fname)]<\/p>\n<p>print(matched_files)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8f93\u51fa\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code>[&#39;file_123.txt&#39;, &#39;abc_456.txt&#39;, &#39;123_abc.txt&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u63d0\u9ad8\u6b63\u5219\u8868\u8fbe\u5f0f\u6548\u7387<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5927\u91cf\u6587\u4ef6\u540d\u65f6\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u6548\u7387\u5c24\u4e3a\u91cd\u8981\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u63d0\u9ad8\u6b63\u5219\u8868\u8fbe\u5f0f\u6548\u7387\u7684\u5efa\u8bae\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u4f7f\u7528\u9884\u7f16\u8bd1\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u5bf9\u8c61<\/strong>\uff1a\u907f\u514d\u5728\u5faa\u73af\u4e2d\u591a\u6b21\u7f16\u8bd1\u6b63\u5219\u8868\u8fbe\u5f0f\u3002<\/li>\n<li><strong>\u4f7f\u7528\u5177\u4f53\u6a21\u5f0f<\/strong>\uff1a\u5c3d\u91cf\u4f7f\u7528\u5177\u4f53\u7684\u5b57\u7b26\u96c6\u548c\u9650\u5b9a\u7b26\uff0c\u907f\u514d\u4f7f\u7528\u6a21\u7cca\u7684\u5339\u914d\u6a21\u5f0f\u3002<\/li>\n<li><strong>\u6d4b\u8bd5\u548c\u4f18\u5316<\/strong>\uff1a\u4f7f\u7528\u5de5\u5177\u6216\u811a\u672c\u6d4b\u8bd5\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u6027\u80fd\uff0c\u5e76\u6839\u636e\u9700\u8981\u8fdb\u884c\u4f18\u5316\u3002<\/li>\n<\/ol>\n<p><p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>import time<\/p>\n<p>pattern = r&#39;^[a-zA-Z0-9_\\-]+\\.(txt|log|csv)$&#39;<\/p>\n<p>regex = re.compile(pattern)<\/p>\n<p>filenames = [&#39;file1.txt&#39;, &#39;file2.log&#39;, &#39;file3.csv&#39;, &#39;file4.doc&#39;, &#39;file5.txt&#39;] * 1000<\/p>\n<p>start_time = time.time()<\/p>\n<p>matched_files = [fname for fname in filenames if regex.match(fname)]<\/p>\n<p>end_time = time.time()<\/p>\n<p>print(f&quot;Matched {len(matched_files)} files in {end_time - start_time:.4f} seconds&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516b\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u6587\u4ef6\u540d\u662f\u4e00\u4e2a\u975e\u5e38\u5b9e\u7528\u7684\u6280\u80fd\u3002\u901a\u8fc7\u5b9a\u4e49\u5408\u9002\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f\u3001\u7f16\u8bd1\u6b63\u5219\u8868\u8fbe\u5f0f\u5bf9\u8c61\u4ee5\u53ca\u4f7f\u7528\u5339\u914d\u51fd\u6570\uff0c\u53ef\u4ee5\u9ad8\u6548\u5730\u5339\u914d\u5404\u79cd\u683c\u5f0f\u7684\u6587\u4ef6\u540d\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6839\u636e\u5177\u4f53\u9700\u6c42\u8c03\u6574\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f\uff0c\u53ef\u4ee5\u89e3\u51b3\u4e0d\u540c\u7684\u6587\u4ef6\u540d\u5339\u914d\u95ee\u9898\u3002\u540c\u65f6\uff0c\u6ce8\u610f\u63d0\u9ad8\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u6548\u7387\uff0c\u786e\u4fdd\u5728\u5904\u7406\u5927\u91cf\u6587\u4ef6\u540d\u65f6\u80fd\u591f\u5feb\u901f\u5b8c\u6210\u5339\u914d\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u5e0c\u671b\u4f60\u80fd\u591f\u638c\u63e1Python\u4e2d\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u6587\u4ef6\u540d\u7684\u57fa\u672c\u65b9\u6cd5\u548c\u6280\u5de7\uff0c\u5e76\u80fd\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\u7075\u6d3b\u5e94\u7528\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u7279\u5b9a\u683c\u5f0f\u7684\u6587\u4ef6\u540d\uff1f<\/strong><br \/>\u8981\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u7279\u5b9a\u683c\u5f0f\u7684\u6587\u4ef6\u540d\uff0c\u60a8\u9700\u8981\u5bfc\u5165<code>re<\/code>\u6a21\u5757\uff0c\u5e76\u7f16\u5199\u4e00\u4e2a\u5339\u914d\u60a8\u9700\u6c42\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u3002\u4f8b\u5982\uff0c\u5982\u679c\u60a8\u60f3\u5339\u914d\u6240\u6709\u4ee5\u201c.txt\u201d\u7ed3\u5c3e\u7684\u6587\u4ef6\u540d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5982\u4e0b\u4ee3\u7801\uff1a  <\/p>\n<pre><code class=\"language-python\">import re\nimport os\n\npattern = r&#39;.*\\.txt$&#39;\nfiles = os.listdir(&#39;your_directory_path&#39;)\n\nmatched_files = [file for file in files if re.match(pattern, file)]\nprint(matched_files)\n<\/code><\/pre>\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>your_directory_path<\/code>\u662f\u60a8\u8981\u68c0\u67e5\u7684\u76ee\u5f55\u8def\u5f84\u3002<\/p>\n<p><strong>\u5728\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u6587\u4ef6\u540d\u65f6\uff0c\u6709\u54ea\u4e9b\u5e38\u89c1\u7684\u9519\u8bef\u9700\u8981\u907f\u514d\uff1f<\/strong><br \/>\u4e00\u4e9b\u5e38\u89c1\u7684\u9519\u8bef\u5305\u62ec\u4f7f\u7528\u4e0d\u6b63\u786e\u7684\u8f6c\u4e49\u5b57\u7b26\u3001\u672a\u8003\u8651\u6587\u4ef6\u540d\u7684\u5927\u5c0f\u5199\u4ee5\u53ca\u6ca1\u6709\u6b63\u786e\u8bbe\u7f6e\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u8fb9\u754c\u3002\u4f8b\u5982\uff0c\u5728\u5904\u7406\u6587\u4ef6\u6269\u5c55\u540d\u65f6\uff0c\u786e\u4fdd\u4f7f\u7528<code>\\.ext<\/code>\u800c\u4e0d\u662f<code>.ext<\/code>\uff0c\u56e0\u4e3a\u540e\u8005\u4f1a\u5339\u914d\u4efb\u4f55\u5b57\u7b26\u3002\u5728\u5339\u914d\u65f6\u8fd8\u53ef\u4ee5\u4f7f\u7528<code>re.IGNORECASE<\/code>\u6807\u5fd7\u6765\u5ffd\u7565\u5927\u5c0f\u5199\u3002<\/p>\n<p><strong>\u5982\u4f55\u63d0\u9ad8\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u6587\u4ef6\u540d\u7684\u6548\u7387\uff1f<\/strong><br \/>\u63d0\u9ad8\u6548\u7387\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u975e\u8d2a\u5a6a\u5339\u914d\uff08\u5982<code>.*?<\/code>\uff09\uff0c\u907f\u514d\u8fc7\u4e8e\u590d\u6742\u7684\u6a21\u5f0f\uff0c\u51cf\u5c11\u4e0d\u5fc5\u8981\u7684\u56de\u6eaf\u3002\u6b64\u5916\uff0c\u53ef\u4ee5\u5229\u7528<code>fnmatch<\/code>\u6a21\u5757\u8fdb\u884c\u7b80\u5355\u7684\u6a21\u5f0f\u5339\u914d\uff0c\u5b83\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u6bd4\u6b63\u5219\u8868\u8fbe\u5f0f\u66f4\u5feb\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>fnmatch.fnmatch(filename, &#39;*.txt&#39;)<\/code>\u6765\u5339\u914d\u6240\u6709\u4ee5\u201c.txt\u201d\u7ed3\u5c3e\u7684\u6587\u4ef6\u540d\u3002\u8fd9\u6837\u53ef\u4ee5\u5728\u6027\u80fd\u548c\u53ef\u8bfb\u6027\u4e4b\u95f4\u627e\u5230\u5e73\u8861\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u5339\u914d\u6587\u4ef6\u540d\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\uff08Regular Expressions, \u7b80\u79f0regex\uff09\u6765\u5b9e [&hellip;]","protected":false},"author":3,"featured_media":1145073,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1145060"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1145060"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1145060\/revisions"}],"predecessor-version":[{"id":1145074,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1145060\/revisions\/1145074"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1145073"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1145060"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1145060"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1145060"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}