{"id":271833,"date":"2024-05-16T13:02:43","date_gmt":"2024-05-16T05:02:43","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/271833.html"},"modified":"2024-05-16T13:02:59","modified_gmt":"2024-05-16T05:02:59","slug":"php-%e9%a1%b9%e7%9b%ae%e7%a8%8b%e5%ba%8f%e4%b8%ad%e7%9a%84-pcre2-%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/271833.html","title":{"rendered":"php \u9879\u76ee\u7a0b\u5e8f\u4e2d\u7684 PCRE2 \u5982\u4f55\u4f7f\u7528"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24194138\/13792283-0531-48ff-937f-3a459575eea8.webp\" alt=\"php \u9879\u76ee\u7a0b\u5e8f\u4e2d\u7684 PCRE2 \u5982\u4f55\u4f7f\u7528\" \/><\/p>\n<p><p>PCRE2\uff08Perl Compatible Regular Expressions \u7b2c\u4e8c\u7248\uff09\u662f\u4e00\u4e2a\u7528\u4e8e\u5b9e\u73b0\u6b63\u5219\u8868\u8fbe\u5f0f\u529f\u80fd\u7684\u5e93\uff0c<strong>\u5b83\u63d0\u4f9b\u4e86\u6a21\u5f0f\u5339\u914d\u7684\u80fd\u529b<\/strong>\u3001<strong>\u5b57\u7b26\u4e32\u641c\u7d22\u548c\u66ff\u6362\u7684\u529f\u80fd<\/strong>\u3001<strong>\u4ee5\u53ca\u66f4\u52a0\u7075\u6d3b\u7684\u5b57\u7b26\u4e32\u5904\u7406\u673a\u5236<\/strong>\u3002\u5728PHP\u9879\u76ee\u7a0b\u5e8f\u4e2d\uff0c\u4f7f\u7528PCRE2\u4e3b\u8981\u4f9d\u9760\u9884\u5b9a\u4e49\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u51fd\u6570\u8fdb\u884c\u3002<strong>\u8fd9\u4e9b\u51fd\u6570\u4ee5\u201cpreg_\u201d\u5f00\u5934<\/strong>\uff0c\u5982<code>preg_match()<\/code>\u3001<code>preg_replace()<\/code>\u7b49\uff0c\u5b83\u4eec\u652f\u6301\u4f7f\u7528PCRE2\u7684\u8bed\u6cd5\u548c\u7279\u6027\u6765\u6267\u884c\u76f8\u5173\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001PCRE2\u7684\u57fa\u7840\u4f7f\u7528<\/h2>\n<\/p>\n<p><p>PCRE2\u5e93\u7684\u57fa\u672c\u4f7f\u7528\u6d89\u53ca\u7f16\u8bd1\u6b63\u5219\u8868\u8fbe\u5f0f\u3001\u5339\u914d\u5b57\u7b26\u4e32\u548c\u6267\u884c\u66ff\u6362\u7b49\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h3><strong>\u7f16\u8bd1\u6b63\u5219\u8868\u8fbe\u5f0f<\/strong><\/h3>\n<\/p>\n<p><p>PCRE2\u5728PHP\u4e2d\u5927\u591a\u662f\u9690\u5f0f\u7f16\u8bd1\u7684\u3002\u5f53\u4f60\u4f7f\u7528\u50cf<code>preg_match()<\/code>\u8fd9\u6837\u7684\u51fd\u6570\u65f6\uff0c\u4f60\u63d0\u4f9b\u7684\u6a21\u5f0f\u5b57\u7b26\u4e32\u5c06\u4f1a\u88ab\u7f16\u8bd1\u6210\u5185\u90e8\u8868\u793a\u5f62\u5f0f\u3002\u8fd9\u4e2a\u7f16\u8bd1\u8fc7\u7a0b\u662f\u81ea\u52a8\u7684\uff0c\u5e76\u4e14\u51fa\u4e8e\u6027\u80fd\u8003\u8651\uff0c\u7f16\u8bd1\u540e\u7684\u6a21\u5f0f\u53ef\u80fd\u4f1a\u88ab\u7f13\u5b58\u3002<\/p>\n<\/p>\n<p><h3><strong>\u5b57\u7b26\u4e32\u5339\u914d<\/strong><\/h3>\n<\/p>\n<p><p>\u5f53\u4f60\u60f3\u5224\u65ad\u4e00\u4e2a\u5b57\u7b26\u4e32\u662f\u5426\u7b26\u5408\u67d0\u4e2a\u6a21\u5f0f\u6216\u60f3\u8981\u4ece\u4e00\u4e2a\u5b57\u7b26\u4e32\u4e2d\u63d0\u53d6\u4fe1\u606f\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>preg_match()<\/code>\u51fd\u6570\u6765\u8fdb\u884c\u5339\u914d\u68c0\u67e5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-php\">$pattern = &#039;\/^test(\\d+)\/i&#039;;<\/p>\n<p>$subject = &#039;Test123&#039;;<\/p>\n<p>if (preg_match($pattern, $subject, $matches)) {<\/p>\n<p>    \/\/ \u5b57\u7b26\u4e32\u5339\u914d\u6210\u529f<\/p>\n<p>    \/\/ $matches[1] \u5305\u542b\u4e86\u6355\u83b7\u7ec4\u4e2d\u7684\u6570\u5b57<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e8c\u3001\u5728PHP\u4e2d\u6267\u884cPCRE2\u5339\u914d<\/h2>\n<\/p>\n<p><h3><strong>\u5b57\u7b26\u4e32\u7684\u57fa\u672c\u5339\u914d<\/strong><\/h3>\n<\/p>\n<p><p><code>preg_match()<\/code>\u51fd\u6570\u7528\u4e8e\u6267\u884c\u4e00\u4e2a\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\uff0c\u68c0\u67e5\u6a21\u5f0f\u662f\u5426\u5728\u6307\u5b9a\u7684\u5b57\u7b26\u4e32\u4e2d\u51fa\u73b0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-php\">$pattern = &#039;\/foo\/&#039;;<\/p>\n<p>$text = &#039;bar foo baz&#039;;<\/p>\n<p>if (preg_match($pattern, $text)) {<\/p>\n<p>    \/\/ \u5339\u914d\u6210\u529f\u7684\u5904\u7406<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3><strong>\u4f7f\u7528\u5339\u914d\u7ed3\u679c<\/strong><\/h3>\n<\/p>\n<p><p><code>preg_match()<\/code>\u51fd\u6570\u80fd\u591f\u5c06\u5339\u914d\u7684\u7ed3\u679c\u4fdd\u5b58\u5728\u4e00\u4e2a\u6570\u7ec4\u4e2d\uff0c\u8fd9\u5141\u8bb8\u4f60\u505a\u8fdb\u4e00\u6b65\u7684\u5904\u7406\u6216\u68c0\u67e5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-php\">$pattern = &#039;\/(\\S+) (\\S+)\/&#039;;<\/p>\n<p>$text = &#039;hello world&#039;;<\/p>\n<p>if (preg_match($pattern, $text, $matches)) {<\/p>\n<p>    \/\/ $matches[0] \u662f\u5b8c\u6574\u7684\u5339\u914d<\/p>\n<p>    \/\/ $matches[1] \u662f\u7b2c\u4e00\u4e2a\u6355\u83b7\u7ec4\u7684\u5339\u914d\uff0c\u5373 &#039;hello&#039;<\/p>\n<p>    \/\/ $matches[2] \u662f\u7b2c\u4e8c\u4e2a\u6355\u83b7\u7ec4\u7684\u5339\u914d\uff0c\u5373 &#039;world&#039;<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e09\u3001\u4f7f\u7528PCRE2\u6267\u884c\u5b57\u7b26\u4e32\u66ff\u6362<\/h2>\n<\/p>\n<p><h3><strong>\u7b80\u5355\u5b57\u7b26\u4e32\u66ff\u6362<\/strong><\/h3>\n<\/p>\n<p><p><code>preg_replace()<\/code>\u51fd\u6570\u662f\u4e00\u4e2a\u975e\u5e38\u5f3a\u5927\u7684\u5b57\u7b26\u4e32\u66ff\u6362\u5de5\u5177\uff0c\u5b83\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u5b9a\u4e49\u9700\u8981\u66ff\u6362\u7684\u6587\u672c\u6a21\u5f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-php\">$pattern = &#039;\/apple\/&#039;;<\/p>\n<p>$replacement = &#039;orange&#039;;<\/p>\n<p>$text = &#039;I like apple.&#039;;<\/p>\n<p>$replacedText = preg_replace($pattern, $replacement, $text);<\/p>\n<p>\/\/ \u7ed3\u679c\uff1a&#039;I like orange.&#039;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3><strong>\u57fa\u4e8e\u6a21\u5f0f\u7684\u9ad8\u7ea7\u66ff\u6362<\/strong><\/h3>\n<\/p>\n<p><p>\u4f7f\u7528\u5e26\u6709\u6355\u83b7\u7ec4\u7684\u6a21\u5f0f\uff0c\u53ef\u4ee5\u5728\u66ff\u6362\u65f6\u5f15\u7528\u8fd9\u4e9b\u7ec4\uff0c\u4ece\u800c\u8fdb\u884c\u66f4\u590d\u6742\u7684\u66ff\u6362\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-php\">$pattern = &#039;\/([a-z]+) ([0-9]+)\/&#039;;<\/p>\n<p>$replacement = &#039;number $2 is used by word $1&#039;;<\/p>\n<p>$text = &#039;abc 123&#039;;<\/p>\n<p>$replacedText = preg_replace($pattern, $replacement, $text);<\/p>\n<p>\/\/ \u7ed3\u679c\uff1a&#039;number 123 is used by word abc&#039;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u56db\u3001PCRE2\u7684\u6a21\u5f0f\u4fee\u9970\u7b26<\/h2>\n<\/p>\n<p><p>PCRE2\u652f\u6301\u591a\u79cd\u6a21\u5f0f\u4fee\u9970\u7b26\uff0c\u5b83\u4eec\u53ef\u4ee5\u6539\u53d8\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u884c\u4e3a\u3002<\/p>\n<\/p>\n<p><h3><strong>\u5e38\u7528\u7684\u6a21\u5f0f\u4fee\u9970\u7b26<\/strong><\/h3>\n<\/p>\n<ul>\n<li><code>i<\/code>\uff08\u4e0d\u533a\u5206\u5927\u5c0f\u5199\uff09<\/li>\n<li><code>m<\/code>\uff08\u591a\u884c\u6a21\u5f0f\uff09<\/li>\n<li><code>s<\/code>\uff08\u4f7f &quot;.&quot; \u5339\u914d\u5305\u62ec\u6362\u884c\u7b26\u5728\u5185\u7684\u6240\u6709\u5b57\u7b26\uff09<\/li>\n<li><code>x<\/code>\uff08\u5141\u8bb8\u4f7f\u7528\u7a7a\u767d\u5b57\u7b26\u548c\u6ce8\u91ca\u6765\u683c\u5f0f\u5316\u6b63\u5219\u8868\u8fbe\u5f0f\uff09<\/li>\n<\/ul>\n<p><pre><code class=\"language-php\">$pattern = &#039;\/^abc\/m&#039;; \/\/ \u591a\u884c\u6a21\u5f0f\uff0c&#039;^&#039;\u53ef\u4ee5\u5339\u914d\u6bcf\u4e00\u884c\u7684\u5f00\u5934<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3><strong>\u4fee\u9970\u7b26\u7684\u7ec4\u5408\u4f7f\u7528<\/strong><\/h3>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u6839\u636e\u9700\u8981\u5c06\u591a\u4e2a\u4fee\u9970\u7b26\u7ec4\u5408\u4f7f\u7528\u3002\u9009\u62e9\u6b63\u786e\u7684\u4fee\u9970\u7b26\u5bf9\u4e8e\u521b\u5efa\u6709\u6548\u548c\u9ad8\u6548\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u975e\u5e38\u5173\u952e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-php\">$pattern = &#039;\/foo\/i&#039;; \/\/ \u4e0d\u533a\u5206\u5927\u5c0f\u5199\u5339\u914d&#039;foo&#039;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>Q\uff1a\u5982\u4f55\u5728 PHP \u9879\u76ee\u7a0b\u5e8f\u4e2d\u4f7f\u7528 PCRE2\uff1f<\/strong><br \/>\n<strong>A\uff1a\u5728 PHP \u9879\u76ee\u7a0b\u5e8f\u4e2d\u4f7f\u7528 PCRE2 \u7684\u65b9\u6cd5\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\n<strong>Q\uff1a\u5982\u4f55\u5728 PHP \u4e2d\u5229\u7528 PCRE2 \u5b9e\u73b0\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\uff1f<\/strong><\/p>\n<p>A\uff1a\u5728 PHP \u9879\u76ee\u7a0b\u5e8f\u4e2d\u4f7f\u7528 PCRE2 \u8fdb\u884c\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u975e\u5e38\u7b80\u5355\u3002\u9996\u5148\uff0c\u4f60\u9700\u8981\u5728 PHP \u4e2d\u542f\u7528 PCRE2 \u6269\u5c55\u3002\u7136\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528 preg_match() \u51fd\u6570\u6216 preg_match_all() \u51fd\u6570\u6765\u8fdb\u884c\u5339\u914d\u3002\u8fd9\u4e9b\u51fd\u6570\u63a5\u53d7\u4e24\u4e2a\u53c2\u6570\uff0c\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f\uff0c\u7b2c\u4e8c\u4e2a\u53c2\u6570\u662f\u8981\u5339\u914d\u7684\u5b57\u7b26\u4e32\u3002\u901a\u8fc7\u4f7f\u7528\u8fd9\u4e9b\u51fd\u6570\uff0c\u4f60\u53ef\u4ee5\u6839\u636e\u81ea\u5df1\u7684\u9700\u6c42\u6765\u5b9e\u73b0\u5b57\u7b26\u4e32\u7684\u5339\u914d\u548c\u66ff\u6362\uff0c\u4ee5\u53ca\u5176\u4ed6\u66f4\u9ad8\u7ea7\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u64cd\u4f5c\u3002<\/p>\n<p>\u4e3e\u4e2a\u4f8b\u5b50\uff0c\u5047\u8bbe\u4f60\u9700\u8981\u4ece\u4e00\u4e2a\u5b57\u7b26\u4e32\u4e2d\u63d0\u53d6\u6240\u6709\u7684\u624b\u673a\u53f7\u7801\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u8fdb\u884c\u5339\u914d\uff1a<\/p>\n<pre><code>$pattern = &quot;\/\\d{11}\/&quot;; \/\/ \u5339\u914d 11 \u4f4d\u6570\u5b57\u7684\u624b\u673a\u53f7\u7801\n$string = &quot;\u6211\u7684\u624b\u673a\u53f7\u7801\u662f\uff1a12345678901\uff0c\u8bf7\u8054\u7cfb\u6211\u3002&quot;;\npreg_match_all($pattern, $string, $matches);\n<\/code><\/pre>\n<p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f\u201c\/\\d{11}\/\u201d\u6765\u5339\u914d 11 \u4f4d\u6570\u5b57\u7684\u624b\u673a\u53f7\u7801\u3002\u7136\u540e\uff0c\u6211\u4eec\u4f7f\u7528 preg_match_all() \u51fd\u6570\u5c06\u5339\u914d\u7ed3\u679c\u5b58\u50a8\u5728 $matches \u53d8\u91cf\u4e2d\u3002\u6700\u540e\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 $matches \u53d8\u91cf\u6765\u83b7\u53d6\u5339\u914d\u5230\u7684\u624b\u673a\u53f7\u7801\u6570\u7ec4\u3002<\/p>\n<p>\u6ce8\u610f\uff0cPCRE2 \u652f\u6301\u5927\u91cf\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u8bed\u6cd5\u548c\u9009\u9879\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u6c42\u8fdb\u884c\u7075\u6d3b\u7684\u8c03\u6574\u548c\u4f7f\u7528\u3002\u4f60\u53ef\u4ee5\u67e5\u9605 PHP \u6587\u6863\u6216\u5176\u4ed6\u76f8\u5173\u8d44\u6e90\u6765\u4e86\u89e3\u66f4\u591a\u5173\u4e8e PCRE2 \u548c\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u7528\u6cd5\u548c\u6280\u5de7\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"PCRE2\uff08Perl Compatible Regular Expressions \u7b2c\u4e8c\u7248\uff09\u662f\u4e00\u4e2a\u7528\u4e8e\u5b9e\u73b0\u6b63\u5219 [&hellip;]","protected":false},"author":3,"featured_media":271855,"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\/271833"}],"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=271833"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/271833\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/271855"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=271833"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=271833"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=271833"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}