{"id":1097670,"date":"2025-01-08T15:14:05","date_gmt":"2025-01-08T07:14:05","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1097670.html"},"modified":"2025-01-08T15:14:07","modified_gmt":"2025-01-08T07:14:07","slug":"python%e5%a6%82%e4%bd%95%e5%8f%96%e5%87%ba%e5%ad%97%e7%ac%a6%e4%b8%ad%e7%9a%84%e6%95%b0%e5%ad%97-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1097670.html","title":{"rendered":"python\u5982\u4f55\u53d6\u51fa\u5b57\u7b26\u4e2d\u7684\u6570\u5b57"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24212334\/a17bdf2c-69b7-4004-b07b-18ed967b3d70.webp\" alt=\"python\u5982\u4f55\u53d6\u51fa\u5b57\u7b26\u4e2d\u7684\u6570\u5b57\" \/><\/p>\n<p><p> <strong>Python\u53d6\u51fa\u5b57\u7b26\u4e2d\u7684\u6570\u5b57<\/strong>\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\uff0c\u4f8b\u5982\uff1a<strong>\u6b63\u5219\u8868\u8fbe\u5f0f\u3001\u5217\u8868\u89e3\u6790\u3001\u5185\u7f6e\u5b57\u7b26\u4e32\u65b9\u6cd5<\/strong>\u3002\u5176\u4e2d\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f\uff08Regular Expressions, \u7b80\u79f0RE\uff09\u662f\u4e00\u79cd\u5f3a\u5927\u800c\u7075\u6d3b\u7684\u6587\u672c\u5904\u7406\u5de5\u5177\uff0c\u80fd\u591f\u9ad8\u6548\u5730\u4ece\u5b57\u7b26\u4e32\u4e2d\u63d0\u53d6\u7b26\u5408\u7279\u5b9a\u6a21\u5f0f\u7684\u5b50\u5b57\u7b26\u4e32\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u51e0\u79cd\u65b9\u6cd5\uff0c\u5e76\u91cd\u70b9\u9610\u8ff0\u5982\u4f55\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u5b9e\u73b0\u8fd9\u4e00\u76ee\u7684\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u6b63\u5219\u8868\u8fbe\u5f0f<\/h3>\n<\/p>\n<p><p>\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u4e00\u79cd\u5339\u914d\u5b57\u7b26\u4e32\u4e2d\u67d0\u4e9b\u6a21\u5f0f\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u7528\u6765\u641c\u7d22\u3001\u7f16\u8f91\u6216\u5904\u7406\u6587\u672c\u3002Python\u7684<code>re<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u5bf9\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u652f\u6301\u3002\u901a\u8fc7\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u6211\u4eec\u53ef\u4ee5\u65b9\u4fbf\u5730\u4ece\u5b57\u7b26\u4e32\u4e2d\u63d0\u53d6\u6570\u5b57\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>def extract_numbers(string):<\/p>\n<p>    numbers = re.findall(r&#39;\\d+&#39;, string)<\/p>\n<p>    return numbers<\/p>\n<p>string = &quot;abc123def456gh789&quot;<\/p>\n<p>print(extract_numbers(string))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>re.findall(r&#39;\\d+&#39;, string)<\/code>\u4f1a\u67e5\u627e\u5e76\u8fd4\u56de\u5b57\u7b26\u4e32\u4e2d\u6240\u6709\u5339\u914d<code>\\d+<\/code>\u6a21\u5f0f\u7684\u90e8\u5206\u3002<code>\\d<\/code>\u8868\u793a\u6570\u5b57\uff0c<code>+<\/code>\u8868\u793a\u524d\u9762\u7684\u5143\u7d20\u81f3\u5c11\u51fa\u73b0\u4e00\u6b21\u3002\u56e0\u6b64\uff0c\u8fd9\u4e2a\u6b63\u5219\u8868\u8fbe\u5f0f\u4f1a\u5339\u914d\u5b57\u7b26\u4e32\u4e2d\u7684\u6240\u6709\u8fde\u7eed\u6570\u5b57\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u5217\u8868\u89e3\u6790<\/h3>\n<\/p>\n<p><p>\u5217\u8868\u89e3\u6790\u662f\u4e00\u79cd\u7b80\u6d01\u4e14\u9ad8\u6548\u7684\u65b9\u6cd5\u6765\u5904\u7406\u5217\u8868\u6570\u636e\u3002\u5728\u5904\u7406\u5b57\u7b26\u4e32\u65f6\uff0c\u5217\u8868\u89e3\u6790\u540c\u6837\u53ef\u4ee5\u7528\u4e8e\u63d0\u53d6\u6570\u5b57\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u5224\u65ad\u5b57\u7b26\u662f\u5426\u4e3a\u6570\u5b57\uff0c\u5c06\u5b57\u7b26\u4e32\u4e2d\u7684\u6240\u6709\u6570\u5b57\u63d0\u53d6\u51fa\u6765\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def extract_numbers(string):<\/p>\n<p>    numbers = [char for char in string if char.isdigit()]<\/p>\n<p>    return numbers<\/p>\n<p>string = &quot;abc123def456gh789&quot;<\/p>\n<p>print(extract_numbers(string))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u5217\u8868\u89e3\u6790\u901a\u8fc7<code>char.isdigit()<\/code>\u6765\u5224\u65ad\u5b57\u7b26\u662f\u5426\u4e3a\u6570\u5b57\uff0c\u5e76\u5c06\u6240\u6709\u6570\u5b57\u5b57\u7b26\u63d0\u53d6\u5230\u4e00\u4e2a\u65b0\u5217\u8868\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u5185\u7f6e\u5b57\u7b26\u4e32\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>Python\u7684\u5b57\u7b26\u4e32\u65b9\u6cd5\u540c\u6837\u53ef\u4ee5\u7528\u4e8e\u63d0\u53d6\u6570\u5b57\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u7684<code>join<\/code>\u548c<code>filter<\/code>\u65b9\u6cd5\u6765\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def extract_numbers(string):<\/p>\n<p>    numbers = &#39;&#39;.join(filter(str.isdigit, string))<\/p>\n<p>    return numbers<\/p>\n<p>string = &quot;abc123def456gh789&quot;<\/p>\n<p>print(extract_numbers(string))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>filter(str.isdigit, string)<\/code>\u4f1a\u8fc7\u6ee4\u51fa\u5b57\u7b26\u4e32\u4e2d\u6240\u6709\u7684\u6570\u5b57\u5b57\u7b26\uff0c\u7136\u540e\u901a\u8fc7<code>join<\/code>\u65b9\u6cd5\u5c06\u8fd9\u4e9b\u5b57\u7b26\u8fde\u63a5\u6210\u4e00\u4e2a\u65b0\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u7efc\u5408\u5e94\u7528<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u66f4\u597d\u5730\u7406\u89e3\u548c\u5e94\u7528\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u7efc\u5408\u8fd0\u7528\u8fd9\u4e9b\u65b9\u6cd5\u6765\u5b9e\u73b0\u4e00\u4e2a\u529f\u80fd\u66f4\u52a0\u5f3a\u5927\u7684\u6570\u5b57\u63d0\u53d6\u51fd\u6570\u3002\u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u6240\u6709\u63d0\u53d6\u5230\u7684\u6570\u5b57\u8f6c\u6362\u6210\u6574\u6570\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u8fd9\u4e9b\u6574\u6570\u7684\u5217\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>def extract_numbers(string):<\/p>\n<p>    # \u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u63d0\u53d6\u6240\u6709\u6570\u5b57\u90e8\u5206<\/p>\n<p>    number_strings = re.findall(r&#39;\\d+&#39;, string)<\/p>\n<p>    # \u5c06\u63d0\u53d6\u5230\u7684\u6570\u5b57\u5b57\u7b26\u4e32\u8f6c\u6362\u6210\u6574\u6570<\/p>\n<p>    numbers = [int(num) for num in number_strings]<\/p>\n<p>    return numbers<\/p>\n<p>string = &quot;abc123def456gh789&quot;<\/p>\n<p>print(extract_numbers(string))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u63d0\u53d6\u6240\u6709\u6570\u5b57\u90e8\u5206\uff0c\u7136\u540e\u5c06\u8fd9\u4e9b\u6570\u5b57\u5b57\u7b26\u4e32\u8f6c\u6362\u6210\u6574\u6570\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u8fd9\u4e9b\u6574\u6570\u7684\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u6027\u80fd\u6bd4\u8f83<\/h3>\n<\/p>\n<p><p>\u4e0d\u540c\u7684\u65b9\u6cd5\u5728\u5904\u7406\u5927\u91cf\u6570\u636e\u65f6\uff0c\u6027\u80fd\u53ef\u80fd\u4f1a\u6709\u6240\u5dee\u5f02\u3002\u4e3a\u4e86\u9009\u62e9\u6700\u9002\u5408\u7684\u89e3\u51b3\u65b9\u6848\uff0c\u6211\u4eec\u53ef\u4ee5\u5bf9\u8fd9\u4e9b\u65b9\u6cd5\u8fdb\u884c\u6027\u80fd\u6bd4\u8f83\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u6027\u80fd\u6d4b\u8bd5\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>import timeit<\/p>\n<p>def extract_numbers_regex(string):<\/p>\n<p>    return re.findall(r&#39;\\d+&#39;, string)<\/p>\n<p>def extract_numbers_list_comp(string):<\/p>\n<p>    return [char for char in string if char.isdigit()]<\/p>\n<p>def extract_numbers_filter(string):<\/p>\n<p>    return &#39;&#39;.join(filter(str.isdigit, string))<\/p>\n<p>string = &quot;abc123def456gh789&quot; * 1000<\/p>\n<p>print(&quot;Regex time:&quot;, timeit.timeit(lambda: extract_numbers_regex(string), number=100))<\/p>\n<p>print(&quot;List comprehension time:&quot;, timeit.timeit(lambda: extract_numbers_list_comp(string), number=100))<\/p>\n<p>print(&quot;Filter time:&quot;, timeit.timeit(lambda: extract_numbers_filter(string), number=100))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u4ee3\u7801\uff0c\u6211\u4eec\u53ef\u4ee5\u6bd4\u8f83\u4e0d\u540c\u65b9\u6cd5\u5728\u5904\u7406\u76f8\u540c\u5b57\u7b26\u4e32\u65f6\u7684\u6267\u884c\u65f6\u95f4\uff0c\u4ece\u800c\u9009\u62e9\u6700\u9002\u5408\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u5904\u7406\u4e0d\u540c\u7c7b\u578b\u7684\u5b57\u7b26\u4e32<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u53ef\u80fd\u4f1a\u9047\u5230\u5404\u79cd\u7c7b\u578b\u7684\u5b57\u7b26\u4e32\uff0c\u5982\u5305\u542b\u8d1f\u6570\u7684\u5c0f\u6570\u3001\u79d1\u5b66\u8ba1\u6570\u6cd5\u8868\u793a\u7684\u6570\u5b57\u7b49\u3002\u4e3a\u4e86\u5904\u7406\u8fd9\u4e9b\u4e0d\u540c\u7c7b\u578b\u7684\u5b57\u7b26\u4e32\uff0c\u6211\u4eec\u53ef\u4ee5\u6269\u5c55\u6211\u4eec\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u6216\u7ed3\u5408\u5176\u4ed6\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h4>\u5904\u7406\u8d1f\u6570\u548c\u5c0f\u6570<\/h4>\n<\/p>\n<p><p>\u4e3a\u4e86\u63d0\u53d6\u8d1f\u6570\u548c\u5c0f\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u4fee\u6539\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u4f7f\u5176\u80fd\u591f\u5339\u914d\u8fd9\u4e9b\u7279\u6b8a\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>def extract_numbers(string):<\/p>\n<p>    pattern = r&#39;-?\\d+\\.?\\d*&#39;<\/p>\n<p>    number_strings = re.findall(pattern, string)<\/p>\n<p>    numbers = [float(num) for num in number_strings]<\/p>\n<p>    return numbers<\/p>\n<p>string = &quot;abc-123.45def-678.9gh0.123&quot;<\/p>\n<p>print(extract_numbers(string))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f<code>-?\\d+\\.?\\d*<\/code>\u80fd\u591f\u5339\u914d\u8d1f\u6570\u548c\u5c0f\u6570\u3002\u5177\u4f53\u6765\u8bf4\uff0c<code>-?<\/code>\u8868\u793a\u8d1f\u53f7\u662f\u53ef\u9009\u7684\uff0c<code>\\d+<\/code>\u8868\u793a\u4e00\u4e2a\u6216\u591a\u4e2a\u6570\u5b57\uff0c<code>\\.?<\/code>\u8868\u793a\u5c0f\u6570\u70b9\u662f\u53ef\u9009\u7684\uff0c<code>\\d*<\/code>\u8868\u793a\u96f6\u4e2a\u6216\u591a\u4e2a\u6570\u5b57\u3002<\/p>\n<\/p>\n<p><h4>\u5904\u7406\u79d1\u5b66\u8ba1\u6570\u6cd5<\/h4>\n<\/p>\n<p><p>\u4e3a\u4e86\u63d0\u53d6\u79d1\u5b66\u8ba1\u6570\u6cd5\u8868\u793a\u7684\u6570\u5b57\uff0c\u6211\u4eec\u53ef\u4ee5\u8fdb\u4e00\u6b65\u6269\u5c55\u6b63\u5219\u8868\u8fbe\u5f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>def extract_numbers(string):<\/p>\n<p>    pattern = r&#39;-?\\d+\\.?\\d*(e-?\\d+)?&#39;<\/p>\n<p>    number_strings = re.findall(pattern, string)<\/p>\n<p>    numbers = [float(num) for num in number_strings if num]<\/p>\n<p>    return numbers<\/p>\n<p>string = &quot;abc-1.23e-4def5.67e8gh-9.01e-2&quot;<\/p>\n<p>print(extract_numbers(string))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f<code>-?\\d+\\.?\\d*(e-?\\d+)?<\/code>\u80fd\u591f\u5339\u914d\u79d1\u5b66\u8ba1\u6570\u6cd5\u8868\u793a\u7684\u6570\u5b57\u3002\u5177\u4f53\u6765\u8bf4\uff0c<code>e-?\\d+<\/code>\u8868\u793a\u79d1\u5b66\u8ba1\u6570\u6cd5\u4e2d\u7684\u6307\u6570\u90e8\u5206\uff0c<code>?<\/code>\u8868\u793a\u524d\u9762\u7684\u90e8\u5206\u662f\u53ef\u9009\u7684\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u5904\u7406\u5b9e\u9645\u5e94\u7528\u4e2d\u7684\u7279\u6b8a\u60c5\u51b5<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u53ef\u80fd\u4f1a\u9047\u5230\u4e00\u4e9b\u7279\u6b8a\u60c5\u51b5\uff0c\u5982\u5305\u542b\u975e\u6570\u5b57\u5b57\u7b26\u7684\u6570\u5b57\u5b57\u7b26\u4e32\u3001\u9700\u8981\u5904\u7406\u4e0d\u540c\u7f16\u7801\u7684\u5b57\u7b26\u4e32\u7b49\u3002\u4e3a\u4e86\u5904\u7406\u8fd9\u4e9b\u7279\u6b8a\u60c5\u51b5\uff0c\u6211\u4eec\u53ef\u4ee5\u7ed3\u5408\u5176\u4ed6Python\u5e93\u548c\u5de5\u5177\u3002<\/p>\n<\/p>\n<p><h4>\u5904\u7406\u5305\u542b\u975e\u6570\u5b57\u5b57\u7b26\u7684\u6570\u5b57\u5b57\u7b26\u4e32<\/h4>\n<\/p>\n<p><p>\u6709\u65f6\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u4ece\u5305\u542b\u975e\u6570\u5b57\u5b57\u7b26\u7684\u5b57\u7b26\u4e32\u4e2d\u63d0\u53d6\u6570\u5b57\u90e8\u5206\u3002\u4f8b\u5982\uff0c\u4ece\u7535\u8bdd\u53f7\u7801\u4e2d\u63d0\u53d6\u6570\u5b57\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>def extract_phone_numbers(string):<\/p>\n<p>    pattern = r&#39;\\d+&#39;<\/p>\n<p>    number_strings = re.findall(pattern, string)<\/p>\n<p>    phone_numbers = &#39;&#39;.join(number_strings)<\/p>\n<p>    return phone_numbers<\/p>\n<p>string = &quot;(123) 456-7890&quot;<\/p>\n<p>print(extract_phone_numbers(string))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u63d0\u53d6\u5b57\u7b26\u4e32\u4e2d\u7684\u6240\u6709\u6570\u5b57\u90e8\u5206\uff0c\u5e76\u5c06\u5b83\u4eec\u8fde\u63a5\u6210\u4e00\u4e2a\u5b8c\u6574\u7684\u7535\u8bdd\u53f7\u7801\u3002<\/p>\n<\/p>\n<p><h4>\u5904\u7406\u4e0d\u540c\u7f16\u7801\u7684\u5b57\u7b26\u4e32<\/h4>\n<\/p>\n<p><p>\u5728\u5904\u7406\u4e0d\u540c\u7f16\u7801\u7684\u5b57\u7b26\u4e32\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Python\u7684<code>encode<\/code>\u548c<code>decode<\/code>\u65b9\u6cd5\u6765\u8f6c\u6362\u5b57\u7b26\u4e32\u7684\u7f16\u7801\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>def extract_numbers(string):<\/p>\n<p>    string = string.encode(&#39;utf-8&#39;).decode(&#39;utf-8&#39;)<\/p>\n<p>    pattern = r&#39;\\d+&#39;<\/p>\n<p>    number_strings = re.findall(pattern, string)<\/p>\n<p>    numbers = [int(num) for num in number_strings]<\/p>\n<p>    return numbers<\/p>\n<p>string = &quot;abc123def456gh789&quot;<\/p>\n<p>print(extract_numbers(string))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5c06\u5b57\u7b26\u4e32\u7f16\u7801\u4e3aUTF-8\u683c\u5f0f\uff0c\u7136\u540e\u518d\u89e3\u7801\u4e3aUTF-8\u683c\u5f0f\uff0c\u4ee5\u786e\u4fdd\u5b57\u7b26\u4e32\u7684\u7f16\u7801\u4e00\u81f4\u6027\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u4ecb\u7ecd\u4e86\u591a\u79cd\u4ece\u5b57\u7b26\u4e32\u4e2d\u63d0\u53d6\u6570\u5b57\u7684\u65b9\u6cd5\uff0c\u5305\u62ec\u6b63\u5219\u8868\u8fbe\u5f0f\u3001\u5217\u8868\u89e3\u6790\u548c\u5185\u7f6e\u5b57\u7b26\u4e32\u65b9\u6cd5\u3002\u6211\u4eec\u91cd\u70b9\u9610\u8ff0\u4e86\u5982\u4f55\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u63d0\u53d6\u5b57\u7b26\u4e32\u4e2d\u7684\u6570\u5b57\uff0c\u5e76\u901a\u8fc7\u5b9e\u9645\u793a\u4f8b\u6f14\u793a\u4e86\u8fd9\u4e9b\u65b9\u6cd5\u7684\u5e94\u7528\u3002\u4e3a\u4e86\u5e94\u5bf9\u4e0d\u540c\u7c7b\u578b\u7684\u5b57\u7b26\u4e32\u548c\u7279\u6b8a\u60c5\u51b5\uff0c\u6211\u4eec\u8fd8\u6269\u5c55\u4e86\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u5e76\u7ed3\u5408\u5176\u4ed6Python\u5de5\u5177\u548c\u5e93\u6765\u5904\u7406\u5b9e\u9645\u5e94\u7528\u4e2d\u7684\u590d\u6742\u573a\u666f\u3002\u5e0c\u671b\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u8bfb\u8005\u80fd\u591f\u638c\u63e1\u4ece\u5b57\u7b26\u4e32\u4e2d\u63d0\u53d6\u6570\u5b57\u7684\u591a\u79cd\u65b9\u6cd5\uff0c\u5e76\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u7075\u6d3b\u8fd0\u7528\u8fd9\u4e9b\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u63d0\u53d6\u5b57\u7b26\u4e32\u4e2d\u7684\u6240\u6709\u6570\u5b57\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5757<code>re<\/code>\u6765\u63d0\u53d6\u5b57\u7b26\u4e32\u4e2d\u7684\u6240\u6709\u6570\u5b57\u3002\u901a\u8fc7<code>re.findall()<\/code>\u51fd\u6570\uff0c\u60a8\u53ef\u4ee5\u627e\u5230\u5b57\u7b26\u4e32\u4e2d\u6240\u6709\u5339\u914d\u7684\u6570\u5b57\u5e8f\u5217\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">import re\n\ntext = &quot;\u57282023\u5e74\uff0cPython\u7684\u4f7f\u7528\u7387\u8fbe\u5230\u4e8675%\uff01&quot;\nnumbers = re.findall(r&#39;\\d+&#39;, text)\nprint(numbers)  # \u8f93\u51fa: [&#39;2023&#39;, &#39;75&#39;]\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u5c06\u8fd4\u56de\u4e00\u4e2a\u5217\u8868\uff0c\u5305\u542b\u5b57\u7b26\u4e32\u4e2d\u6240\u6709\u7684\u6570\u5b57\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u63d0\u53d6\u5b57\u7b26\u4e32\u4e2d\u6570\u5b57\u7684\u7c7b\u578b\u6216\u683c\u5f0f\uff1f<\/strong><br \/>\u662f\u7684\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u8c03\u6574\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u63d0\u53d6\u7279\u5b9a\u683c\u5f0f\u7684\u6570\u5b57\u3002\u4f8b\u5982\uff0c\u5982\u679c\u53ea\u60f3\u63d0\u53d6\u6d6e\u70b9\u6570\u6216\u5e26\u6709\u5343\u4f4d\u5206\u9694\u7b26\u7684\u6570\u5b57\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e0d\u540c\u7684\u6a21\u5f0f\u3002\u4ee5\u4e0b\u662f\u63d0\u53d6\u6d6e\u70b9\u6570\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">float_numbers = re.findall(r&#39;\\d+\\.\\d+&#39;, text)\nprint(float_numbers)  # \u8f93\u51fa: []\n<\/code><\/pre>\n<p>\u8fd9\u5c06\u8fd4\u56de\u4e00\u4e2a\u5217\u8868\uff0c\u5305\u542b\u6240\u6709\u7b26\u5408\u6d6e\u70b9\u6570\u683c\u5f0f\u7684\u6570\u5b57\u3002<\/p>\n<p><strong>\u4f7f\u7528\u5176\u4ed6\u65b9\u6cd5\u63d0\u53d6\u5b57\u7b26\u4e32\u4e2d\u7684\u6570\u5b57\u6709\u54ea\u4e9b\uff1f<\/strong><br \/>\u9664\u4e86\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u60a8\u8fd8\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u548c\u5b57\u7b26\u4e32\u65b9\u6cd5\u6765\u63d0\u53d6\u6570\u5b57\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u904d\u5386\u5b57\u7b26\u4e32\u5e76\u68c0\u67e5\u6bcf\u4e2a\u5b57\u7b26\u662f\u5426\u4e3a\u6570\u5b57\uff1a<\/p>\n<pre><code class=\"language-python\">text = &quot;Python 3.8\u7248\u672c\u53d1\u5e03\u4e8e2020\u5e74&quot;\nnumbers = [char for char in text if char.isdigit()]\nprint(numbers)  # \u8f93\u51fa: [&#39;3&#39;, &#39;8&#39;, &#39;2&#39;, &#39;0&#39;, &#39;2&#39;, &#39;0&#39;]\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u4f1a\u5c06\u6bcf\u4e2a\u6570\u5b57\u5b57\u7b26\u63d0\u53d6\u5230\u4e00\u4e2a\u5217\u8868\u4e2d\uff0c\u4fbf\u4e8e\u540e\u7eed\u5904\u7406\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u53d6\u51fa\u5b57\u7b26\u4e2d\u7684\u6570\u5b57\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\uff0c\u4f8b\u5982\uff1a\u6b63\u5219\u8868\u8fbe\u5f0f\u3001\u5217\u8868\u89e3\u6790\u3001\u5185\u7f6e\u5b57\u7b26\u4e32\u65b9\u6cd5\u3002\u5176\u4e2d\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f\uff08 [&hellip;]","protected":false},"author":3,"featured_media":1097676,"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\/1097670"}],"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=1097670"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1097670\/revisions"}],"predecessor-version":[{"id":1097677,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1097670\/revisions\/1097677"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1097676"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1097670"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1097670"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1097670"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}