{"id":1135306,"date":"2025-01-08T21:24:01","date_gmt":"2025-01-08T13:24:01","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1135306.html"},"modified":"2025-01-08T21:24:04","modified_gmt":"2025-01-08T13:24:04","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e8%bf%94%e5%9b%9e%e5%ad%97%e7%ac%a6%e4%b8%b2%e4%b8%ad%e7%9a%84%e6%95%b0%e5%ad%97","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1135306.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u8fd4\u56de\u5b57\u7b26\u4e32\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\/25104004\/e6f961af-e8db-4204-bee8-9fbc673195ab.webp\" alt=\"python\u4e2d\u5982\u4f55\u8fd4\u56de\u5b57\u7b26\u4e32\u4e2d\u7684\u6570\u5b57\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u4ece\u5b57\u7b26\u4e32\u4e2d\u63d0\u53d6\u6570\u5b57<\/strong>\u3002\u5e38\u89c1\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u3001\u5b57\u7b26\u4e32\u89e3\u6790\u548c\u5217\u8868\u89e3\u6790\u7b49\u3002<strong>\u6b63\u5219\u8868\u8fbe\u5f0f<\/strong>\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u9002\u5408\u5904\u7406\u590d\u6742\u7684\u6587\u672c\u5339\u914d\u548c\u63d0\u53d6\u4efb\u52a1\u3002<strong>\u5b57\u7b26\u4e32\u89e3\u6790<\/strong>\u548c<strong>\u5217\u8868\u89e3\u6790<\/strong>\u5219\u66f4\u9002\u5408\u7b80\u5355\u7684\u63d0\u53d6\u4efb\u52a1\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5e76\u63d0\u4f9b\u4ee3\u7801\u793a\u4f8b\u548c\u4e13\u4e1a\u89c1\u89e3\uff0c\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u548c\u5e94\u7528\u8fd9\u4e9b\u6280\u672f\u3002<\/p>\n<\/p>\n<hr>\n<p><h2>\u4e00\u3001\u6b63\u5219\u8868\u8fbe\u5f0f<\/h2>\n<\/p>\n<p><p>\u6b63\u5219\u8868\u8fbe\u5f0f\uff08Regular Expressions\uff0c\u7b80\u79f0 regex \u6216 re\uff09\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6587\u672c\u5904\u7406\u5de5\u5177\uff0c\u80fd\u591f\u65b9\u4fbf\u5730\u8fdb\u884c\u590d\u6742\u7684\u6a21\u5f0f\u5339\u914d\u548c\u6587\u672c\u63d0\u53d6\u3002Python \u63d0\u4f9b\u4e86 <code>re<\/code> \u6a21\u5757\u6765\u5904\u7406\u6b63\u5219\u8868\u8fbe\u5f0f\u3002<\/p>\n<\/p>\n<p><h3>1\u3001\u4f7f\u7528 <code>re.findall<\/code> \u63d0\u53d6\u6240\u6709\u6570\u5b57<\/h3>\n<\/p>\n<p><p><code>re.findall<\/code> \u662f\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5757\u4e2d\u7684\u4e00\u4e2a\u51fd\u6570\uff0c\u7528\u4e8e\u67e5\u627e\u6240\u6709\u5339\u914d\u7684\u5b50\u4e32\uff0c\u5e76\u4ee5\u5217\u8868\u7684\u5f62\u5f0f\u8fd4\u56de\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>def extract_numbers(s):<\/p>\n<p>    # \u5b9a\u4e49\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f<\/p>\n<p>    pattern = r&#39;\\d+&#39;<\/p>\n<p>    # \u4f7f\u7528 findall \u67e5\u627e\u6240\u6709\u6570\u5b57<\/p>\n<p>    numbers = re.findall(pattern, s)<\/p>\n<p>    return numbers<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>s = &quot;\u6211\u67092\u4e2a\u82f9\u679c\u548c3\u4e2a\u6a59\u5b50\uff0c\u603b\u5171\u67095\u4e2a\u6c34\u679c\u3002&quot;<\/p>\n<p>numbers = extract_numbers(s)<\/p>\n<p>print(numbers)  # \u8f93\u51fa: [&#39;2&#39;, &#39;3&#39;, &#39;5&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u89e3\u6790<\/strong>: \u8fd9\u91cc\u7684\u6b63\u5219\u8868\u8fbe\u5f0f <code>\\d+<\/code> \u5339\u914d\u4e00\u4e2a\u6216\u591a\u4e2a\u8fde\u7eed\u7684\u6570\u5b57\u5b57\u7b26\u3002<code>re.findall<\/code> \u4f1a\u8fd4\u56de\u6240\u6709\u5339\u914d\u7684\u5b50\u4e32\u3002<\/p>\n<\/p>\n<p><h3>2\u3001\u4f7f\u7528 <code>re.search<\/code> \u63d0\u53d6\u7b2c\u4e00\u4e2a\u6570\u5b57<\/h3>\n<\/p>\n<p><p><code>re.search<\/code> \u7528\u4e8e\u67e5\u627e\u7b2c\u4e00\u4e2a\u5339\u914d\u7684\u5b50\u4e32\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u5339\u914d\u5bf9\u8c61\u3002\u5982\u679c\u53ea\u9700\u8981\u63d0\u53d6\u7b2c\u4e00\u4e2a\u6570\u5b57\uff0c\u53ef\u4ee5\u4f7f\u7528\u8fd9\u4e2a\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>def extract_first_number(s):<\/p>\n<p>    # \u5b9a\u4e49\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f<\/p>\n<p>    pattern = r&#39;\\d+&#39;<\/p>\n<p>    # \u4f7f\u7528 search \u67e5\u627e\u7b2c\u4e00\u4e2a\u6570\u5b57<\/p>\n<p>    match = re.search(pattern, s)<\/p>\n<p>    if match:<\/p>\n<p>        return match.group()<\/p>\n<p>    return None<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>s = &quot;\u6211\u67092\u4e2a\u82f9\u679c\u548c3\u4e2a\u6a59\u5b50\uff0c\u603b\u5171\u67095\u4e2a\u6c34\u679c\u3002&quot;<\/p>\n<p>first_number = extract_first_number(s)<\/p>\n<p>print(first_number)  # \u8f93\u51fa: &#39;2&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u89e3\u6790<\/strong>: \u8fd9\u91cc\u7684 <code>re.search<\/code> \u4f1a\u8fd4\u56de\u7b2c\u4e00\u4e2a\u5339\u914d\u7684\u5b50\u4e32\u3002<code>match.group()<\/code> \u8fd4\u56de\u5339\u914d\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<hr>\n<p><h2>\u4e8c\u3001\u5b57\u7b26\u4e32\u89e3\u6790<\/h2>\n<\/p>\n<p><p>\u5b57\u7b26\u4e32\u89e3\u6790\u662f\u901a\u8fc7\u904d\u5386\u5b57\u7b26\u4e32\u7684\u6bcf\u4e00\u4e2a\u5b57\u7b26\uff0c\u6839\u636e\u67d0\u4e9b\u6761\u4ef6\u5c06\u7b26\u5408\u8981\u6c42\u7684\u5b57\u7b26\u63d0\u53d6\u51fa\u6765\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u5408\u7b80\u5355\u7684\u63d0\u53d6\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><h3>1\u3001\u4f7f\u7528\u5b57\u7b26\u4e32\u89e3\u6790\u63d0\u53d6\u6240\u6709\u6570\u5b57<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">def extract_numbers(s):<\/p>\n<p>    numbers = []<\/p>\n<p>    current_number = &#39;&#39;<\/p>\n<p>    for char in s:<\/p>\n<p>        if char.isdigit():<\/p>\n<p>            current_number += char<\/p>\n<p>        else:<\/p>\n<p>            if current_number:<\/p>\n<p>                numbers.append(current_number)<\/p>\n<p>                current_number = &#39;&#39;<\/p>\n<p>    if current_number:<\/p>\n<p>        numbers.append(current_number)<\/p>\n<p>    return numbers<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>s = &quot;\u6211\u67092\u4e2a\u82f9\u679c\u548c3\u4e2a\u6a59\u5b50\uff0c\u603b\u5171\u67095\u4e2a\u6c34\u679c\u3002&quot;<\/p>\n<p>numbers = extract_numbers(s)<\/p>\n<p>print(numbers)  # \u8f93\u51fa: [&#39;2&#39;, &#39;3&#39;, &#39;5&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u89e3\u6790<\/strong>: \u8fd9\u91cc\u901a\u8fc7\u904d\u5386\u5b57\u7b26\u4e32\u4e2d\u7684\u6bcf\u4e00\u4e2a\u5b57\u7b26\uff0c\u4f7f\u7528 <code>isdigit<\/code> \u65b9\u6cd5\u5224\u65ad\u5b57\u7b26\u662f\u5426\u4e3a\u6570\u5b57\u3002\u5982\u679c\u662f\u6570\u5b57\uff0c\u5c06\u5176\u52a0\u5165\u5f53\u524d\u7684\u6570\u5b57\u5b57\u7b26\u4e32\uff1b\u5982\u679c\u4e0d\u662f\u6570\u5b57\uff0c\u5c06\u5f53\u524d\u7684\u6570\u5b57\u5b57\u7b26\u4e32\u52a0\u5165\u7ed3\u679c\u5217\u8868\u5e76\u91cd\u7f6e\u3002<\/p>\n<\/p>\n<hr>\n<p><h2>\u4e09\u3001\u5217\u8868\u89e3\u6790<\/h2>\n<\/p>\n<p><p>\u5217\u8868\u89e3\u6790\u662f\u4e00\u79cd\u7b80\u6d01\u7684\u65b9\u5f0f\uff0c\u53ef\u4ee5\u7ed3\u5408\u5176\u4ed6\u65b9\u6cd5\u9ad8\u6548\u5730\u5904\u7406\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><h3>1\u3001\u4f7f\u7528\u5217\u8868\u89e3\u6790\u548c <code>isdigit<\/code> \u65b9\u6cd5\u63d0\u53d6\u6240\u6709\u5355\u4e2a\u6570\u5b57<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">def extract_numbers(s):<\/p>\n<p>    return [char for char in s if char.isdigit()]<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>s = &quot;\u6211\u67092\u4e2a\u82f9\u679c\u548c3\u4e2a\u6a59\u5b50\uff0c\u603b\u5171\u67095\u4e2a\u6c34\u679c\u3002&quot;<\/p>\n<p>numbers = extract_numbers(s)<\/p>\n<p>print(numbers)  # \u8f93\u51fa: [&#39;2&#39;, &#39;3&#39;, &#39;5&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u89e3\u6790<\/strong>: \u8fd9\u91cc\u901a\u8fc7\u5217\u8868\u89e3\u6790\uff0c\u904d\u5386\u5b57\u7b26\u4e32\u4e2d\u7684\u6bcf\u4e00\u4e2a\u5b57\u7b26\uff0c\u4f7f\u7528 <code>isdigit<\/code> \u65b9\u6cd5\u5224\u65ad\u5b57\u7b26\u662f\u5426\u4e3a\u6570\u5b57\uff0c\u5e76\u5c06\u5176\u52a0\u5165\u7ed3\u679c\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h3>2\u3001\u7ed3\u5408\u6b63\u5219\u8868\u8fbe\u5f0f\u548c\u5217\u8868\u89e3\u6790\u63d0\u53d6\u6240\u6709\u6570\u5b57<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>def extract_numbers(s):<\/p>\n<p>    pattern = r&#39;\\d+&#39;<\/p>\n<p>    return [match.group() for match in re.finditer(pattern, s)]<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>s = &quot;\u6211\u67092\u4e2a\u82f9\u679c\u548c3\u4e2a\u6a59\u5b50\uff0c\u603b\u5171\u67095\u4e2a\u6c34\u679c\u3002&quot;<\/p>\n<p>numbers = extract_numbers(s)<\/p>\n<p>print(numbers)  # \u8f93\u51fa: [&#39;2&#39;, &#39;3&#39;, &#39;5&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u89e3\u6790<\/strong>: \u8fd9\u91cc\u7ed3\u5408\u4e86\u6b63\u5219\u8868\u8fbe\u5f0f\u548c\u5217\u8868\u89e3\u6790\uff0c\u901a\u8fc7 <code>re.finditer<\/code> \u67e5\u627e\u6240\u6709\u5339\u914d\u7684\u5b50\u4e32\uff0c\u5e76\u4f7f\u7528\u5217\u8868\u89e3\u6790\u5c06\u5176\u63d0\u53d6\u51fa\u6765\u3002<\/p>\n<\/p>\n<hr>\n<p><h2>\u56db\u3001\u7efc\u5408\u6bd4\u8f83\u4e0e\u9009\u62e9<\/h2>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u9700\u6c42\u548c\u573a\u666f\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u6b63\u5219\u8868\u8fbe\u5f0f<\/strong>\uff1a\u9002\u5408\u5904\u7406\u590d\u6742\u7684\u6587\u672c\u5339\u914d\u548c\u63d0\u53d6\u4efb\u52a1\uff0c\u7075\u6d3b\u6027\u5f3a\uff0c\u4f46\u5b66\u4e60\u6210\u672c\u8f83\u9ad8\u3002<\/li>\n<li><strong>\u5b57\u7b26\u4e32\u89e3\u6790<\/strong>\uff1a\u9002\u5408\u7b80\u5355\u7684\u63d0\u53d6\u4efb\u52a1\uff0c\u4ee3\u7801\u8f83\u4e3a\u76f4\u89c2\uff0c\u4f46\u5bf9\u4e8e\u590d\u6742\u7684\u6a21\u5f0f\u5339\u914d\u80fd\u529b\u6709\u9650\u3002<\/li>\n<li><strong>\u5217\u8868\u89e3\u6790<\/strong>\uff1a\u7ed3\u5408\u5176\u4ed6\u65b9\u6cd5\uff0c\u53ef\u4ee5\u7b80\u6d01\u3001\u9ad8\u6548\u5730\u5904\u7406\u5b57\u7b26\u4e32\uff0c\u4f46\u5bf9\u4e8e\u590d\u6742\u7684\u63d0\u53d6\u4efb\u52a1\u4ecd\u9700\u4f9d\u8d56\u5176\u4ed6\u6280\u672f\u3002<\/li>\n<\/ol>\n<p><h3>\u5b9e\u8df5\u5efa\u8bae<\/h3>\n<\/p>\n<ul>\n<li><strong>\u7b80\u5355\u4efb\u52a1<\/strong>\uff1a\u5982\u679c\u4ec5\u9700\u8981\u63d0\u53d6\u5b57\u7b26\u4e32\u4e2d\u7684\u6570\u5b57\u5b57\u7b26\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u89e3\u6790\u6216\u5217\u8868\u89e3\u6790\u3002<\/li>\n<li><strong>\u590d\u6742\u4efb\u52a1<\/strong>\uff1a\u5982\u679c\u9700\u8981\u5904\u7406\u590d\u6742\u7684\u6587\u672c\u6a21\u5f0f\u5339\u914d\u548c\u63d0\u53d6\u4efb\u52a1\uff0c\u5efa\u8bae\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u3002<\/li>\n<li><strong>\u6027\u80fd\u9700\u6c42<\/strong>\uff1a\u5728\u9700\u8981\u5904\u7406\u5927\u91cf\u6570\u636e\u6216\u5bf9\u6027\u80fd\u6709\u8f83\u9ad8\u8981\u6c42\u65f6\uff0c\u5efa\u8bae\u8fdb\u884c\u6027\u80fd\u6d4b\u8bd5\uff0c\u9009\u62e9\u6700\u5408\u9002\u7684\u65b9\u6cd5\u3002<\/li>\n<\/ul>\n<p><h3>\u4ee3\u7801\u793a\u4f8b<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801\uff1a\u591a\u79cd\u65b9\u6cd5\u63d0\u53d6\u5b57\u7b26\u4e32\u4e2d\u7684\u6570\u5b57<\/p>\n<p>s = &quot;\u6211\u67092\u4e2a\u82f9\u679c\u548c3\u4e2a\u6a59\u5b50\uff0c\u603b\u5171\u67095\u4e2a\u6c34\u679c\u3002&quot;<\/p>\n<h2><strong>\u65b9\u6cd51\uff1a\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f<\/strong><\/h2>\n<p>import re<\/p>\n<p>def extract_numbers_re(s):<\/p>\n<p>    pattern = r&#39;\\d+&#39;<\/p>\n<p>    return re.findall(pattern, s)<\/p>\n<p>numbers_re = extract_numbers_re(s)<\/p>\n<p>print(&quot;\u6b63\u5219\u8868\u8fbe\u5f0f\u63d0\u53d6\u7ed3\u679c:&quot;, numbers_re)<\/p>\n<h2><strong>\u65b9\u6cd52\uff1a\u4f7f\u7528\u5b57\u7b26\u4e32\u89e3\u6790<\/strong><\/h2>\n<p>def extract_numbers_str_parse(s):<\/p>\n<p>    numbers = []<\/p>\n<p>    current_number = &#39;&#39;<\/p>\n<p>    for char in s:<\/p>\n<p>        if char.isdigit():<\/p>\n<p>            current_number += char<\/p>\n<p>        else:<\/p>\n<p>            if current_number:<\/p>\n<p>                numbers.append(current_number)<\/p>\n<p>                current_number = &#39;&#39;<\/p>\n<p>    if current_number:<\/p>\n<p>        numbers.append(current_number)<\/p>\n<p>    return numbers<\/p>\n<p>numbers_str_parse = extract_numbers_str_parse(s)<\/p>\n<p>print(&quot;\u5b57\u7b26\u4e32\u89e3\u6790\u63d0\u53d6\u7ed3\u679c:&quot;, numbers_str_parse)<\/p>\n<h2><strong>\u65b9\u6cd53\uff1a\u4f7f\u7528\u5217\u8868\u89e3\u6790<\/strong><\/h2>\n<p>def extract_numbers_list_comp(s):<\/p>\n<p>    return [char for char in s if char.isdigit()]<\/p>\n<p>numbers_list_comp = extract_numbers_list_comp(s)<\/p>\n<p>print(&quot;\u5217\u8868\u89e3\u6790\u63d0\u53d6\u7ed3\u679c:&quot;, numbers_list_comp)<\/p>\n<h2><strong>\u65b9\u6cd54\uff1a\u7ed3\u5408\u6b63\u5219\u8868\u8fbe\u5f0f\u548c\u5217\u8868\u89e3\u6790<\/strong><\/h2>\n<p>def extract_numbers_combined(s):<\/p>\n<p>    pattern = r&#39;\\d+&#39;<\/p>\n<p>    return [match.group() for match in re.finditer(pattern, s)]<\/p>\n<p>numbers_combined = extract_numbers_combined(s)<\/p>\n<p>print(&quot;\u7ed3\u5408\u65b9\u6cd5\u63d0\u53d6\u7ed3\u679c:&quot;, numbers_combined)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u603b\u7ed3<\/strong>: \u638c\u63e1\u591a\u79cd\u63d0\u53d6\u5b57\u7b26\u4e32\u4e2d\u6570\u5b57\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5e2e\u52a9\u4f60\u5728\u4e0d\u540c\u7684\u5e94\u7528\u573a\u666f\u4e0b\u7075\u6d3b\u9009\u62e9\u6700\u5408\u9002\u7684\u6280\u672f\u624b\u6bb5\uff0c\u63d0\u9ad8\u4ee3\u7801\u7684\u6548\u7387\u548c\u53ef\u8bfb\u6027\u3002\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u6839\u636e\u5177\u4f53\u9700\u6c42\u548c\u6027\u80fd\u8981\u6c42\u9009\u62e9\u6700\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u662f\u9ad8\u6548\u89e3\u51b3\u95ee\u9898\u7684\u5173\u952e\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Python\u4e2d\uff0c\u6709\u54ea\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u4ece\u5b57\u7b26\u4e32\u4e2d\u63d0\u53d6\u6570\u5b57\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u63d0\u53d6\u5b57\u7b26\u4e32\u4e2d\u7684\u6570\u5b57\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u3002\u5e38\u89c1\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u4ee5\u53ca\u5b57\u7b26\u4e32\u7684\u5185\u7f6e\u65b9\u6cd5\u3002\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u53ef\u4ee5\u901a\u8fc7<code>re<\/code>\u6a21\u5757\u7684<code>findall()<\/code>\u51fd\u6570\u6765\u5b9e\u73b0\uff0c\u4f8b\u5982\uff1a<code>re.findall(r&#39;\\d+&#39;, string)<\/code>\uff0c\u8fd9\u5c06\u8fd4\u56de\u5b57\u7b26\u4e32\u4e2d\u6240\u6709\u6570\u5b57\u7684\u5217\u8868\u3002\u6b64\u5916\uff0c\u5229\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u548c<code>isdigit()<\/code>\u65b9\u6cd5\u4e5f\u53ef\u4ee5\u7b80\u5355\u5730\u8fc7\u6ee4\u51fa\u6570\u5b57\u5b57\u7b26\u3002<\/p>\n<p><strong>\u5982\u679c\u5b57\u7b26\u4e32\u4e2d\u5305\u542b\u8d1f\u6570\u6216\u5c0f\u6570\uff0c\u5982\u4f55\u63d0\u53d6\u8fd9\u4e9b\u6570\u503c\uff1f<\/strong><br \/>\u63d0\u53d6\u8d1f\u6570\u6216\u5c0f\u6570\u9700\u8981\u7a0d\u5fae\u590d\u6742\u4e00\u4e9b\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u3002\u53ef\u4ee5\u4f7f\u7528<code>re<\/code>\u6a21\u5757\u4e2d\u7684\u6a21\u5f0f\uff0c\u5982<code>r&#39;-?\\d+\\.?\\d*&#39;<\/code>\uff0c\u6765\u5339\u914d\u8d1f\u6570\u548c\u5c0f\u6570\u3002\u8fd9\u6bb5\u6b63\u5219\u8868\u8fbe\u5f0f\u4f1a\u6355\u83b7\u5e26\u8d1f\u53f7\u7684\u6570\u5b57\u4ee5\u53ca\u5305\u542b\u5c0f\u6570\u70b9\u7684\u6570\u503c\uff0c\u4ece\u800c\u6ee1\u8db3\u66f4\u590d\u6742\u7684\u6570\u5b57\u63d0\u53d6\u9700\u6c42\u3002<\/p>\n<p><strong>\u5982\u4f55\u5c06\u63d0\u53d6\u7684\u6570\u5b57\u8f6c\u6362\u4e3a\u6574\u6570\u6216\u6d6e\u70b9\u6570\uff1f<\/strong><br \/>\u5728\u63d0\u53d6\u51fa\u5b57\u7b26\u4e32\u4e2d\u7684\u6570\u5b57\u540e\uff0c\u901a\u5e38\u9700\u8981\u5c06\u5176\u8f6c\u6362\u4e3a\u6574\u6570\u6216\u6d6e\u70b9\u6570\u3002\u53ef\u4ee5\u4f7f\u7528<code>int()<\/code>\u51fd\u6570\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570\uff0c\u4f7f\u7528<code>float()<\/code>\u51fd\u6570\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6d6e\u70b9\u6570\u3002\u4f8b\u5982\uff0c\u63d0\u53d6\u5230\u7684\u6570\u5b57\u5b57\u7b26\u4e32\u53ef\u4ee5\u901a\u8fc7<code>int(num_string)<\/code>\u6216<code>float(num_string)<\/code>\u8fdb\u884c\u8f6c\u6362\u3002\u6ce8\u610f\uff0c\u5728\u8f6c\u6362\u4e4b\u524d\u5e94\u786e\u4fdd\u5b57\u7b26\u4e32\u662f\u6709\u6548\u7684\u6570\u5b57\u683c\u5f0f\uff0c\u4ee5\u907f\u514d\u5f15\u53d1\u5f02\u5e38\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u4ece\u5b57\u7b26\u4e32\u4e2d\u63d0\u53d6\u6570\u5b57\u3002\u5e38\u89c1\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u3001\u5b57\u7b26\u4e32\u89e3\u6790\u548c\u5217\u8868\u89e3\u6790\u7b49\u3002\u6b63 [&hellip;]","protected":false},"author":3,"featured_media":1135312,"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\/1135306"}],"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=1135306"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1135306\/revisions"}],"predecessor-version":[{"id":1135313,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1135306\/revisions\/1135313"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1135312"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1135306"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1135306"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1135306"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}