{"id":239,"date":"2023-02-24T05:59:31","date_gmt":"2023-02-23T21:59:31","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=239"},"modified":"2023-04-30T15:20:44","modified_gmt":"2023-04-30T07:20:44","slug":"mysql-regular-expressions","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/24\/mysql-regular-expressions\/","title":{"rendered":"MySQL \u6b63\u5219\u8868\u8fbe\u5f0f"},"content":{"rendered":"<p>\u6211\u4eec\u5df2\u7ecf\u4e86\u89e3\u5230MySQL\u53ef\u4ee5\u901a\u8fc7<code>LIKE ...%<\/code>\u6765\u8fdb\u884c\u6a21\u7cca\u5339\u914d\u3002<\/p>\n<p>MySQL\u540c\u6837\u4e5f\u652f\u6301\u5176\u4ed6\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u5339\u914d\uff0cMySQL\u4e2d\u4f7f\u7528<code>REGEXP<\/code>\u64cd\u4f5c\u7b26\u6765\u8fdb\u884c\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u3002<\/p>\n<p>\u4e0b\u8868\u4e2d\u7684\u6b63\u5219\u6a21\u5f0f\u53ef\u5e94\u7528\u4e8e<code>REGEXP<\/code>\u64cd\u4f5c\u7b26\u4e2d<\/p>\n<p><!-- more --><\/p>\n<table>\n<thead>\n<tr>\n<th style=\"text-align: left;\">\u6a21\u5f0f<\/th>\n<th style=\"text-align: left;\">\u63cf\u8ff0<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"text-align: left;\"><code>^<\/code><\/td>\n<td style=\"text-align: left;\">\u5339\u914d\u8f93\u5165\u5b57\u7b26\u4e32\u7684\u5f00\u59cb\u4f4d\u7f6e\u3002<br \/>\u5982\u679c\u8bbe\u7f6e\u4e86 RegExp \u5bf9\u8c61\u7684 Multiline \u5c5e\u6027\uff0c<code>^<\/code>\u4e5f\u5339\u914d<code>\\n<\/code>\u6216<code>\\r<\/code>\u4e4b\u540e\u7684\u4f4d\u7f6e<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\"><code>$<\/code><\/td>\n<td style=\"text-align: left;\">\u5339\u914d\u8f93\u5165\u5b57\u7b26\u4e32\u7684\u7ed3\u675f\u4f4d\u7f6e\u3002<br \/>\u5982\u679c\u8bbe\u7f6e\u4e86RegExp \u5bf9\u8c61\u7684 Multiline \u5c5e\u6027\uff0c$ \u4e5f\u5339\u914d<code>\\n<\/code>\u6216<code>\\r<\/code>\u4e4b\u524d\u7684\u4f4d\u7f6e<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\"><code>.<\/code><\/td>\n<td style=\"text-align: left;\">\u5339\u914d\u9664<code>\\n<\/code>\u4e4b\u5916\u7684\u4efb\u4f55\u5355\u4e2a\u5b57\u7b26\u3002<br \/>\u8981\u5339\u914d\u5305\u62ec<code>\\n<\/code>\u5728\u5185\u7684\u4efb\u4f55\u5b57\u7b26\uff0c\u8bf7\u4f7f\u7528\u50cf<code>[.\\n]<\/code>\u7684\u6a21\u5f0f<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\"><code>[...]<\/code><\/td>\n<td style=\"text-align: left;\">\u5b57\u7b26\u96c6\u5408\u3002\u5339\u914d\u6240\u5305\u542b\u7684\u4efb\u610f\u4e00\u4e2a\u5b57\u7b26\u3002<br \/>\u4f8b\u5982\uff0c<code>[abc]<\/code>\u53ef\u4ee5\u5339\u914d<code>plain<\/code>\u4e2d\u7684<code>a<\/code><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\"><code>[^...]<\/code><\/td>\n<td style=\"text-align: left;\">\u8d1f\u503c\u5b57\u7b26\u96c6\u5408\u3002\u5339\u914d\u672a\u5305\u542b\u7684\u4efb\u610f\u5b57\u7b26\u3002<br \/>\u4f8b\u5982\uff0c<code>[^abc]<\/code>\u53ef\u4ee5\u5339\u914d<code>plain<\/code>\u4e2d\u7684<code>p<\/code><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\"><code>p1\\|p2\\|p3<\/code><\/td>\n<td style=\"text-align: left;\">\u5339\u914d p1 \u6216 p2 \u6216 p3\u3002<br \/>\u4f8b\u5982\uff0c<code>z\\|food<\/code> \u80fd\u5339\u914d<code>z<\/code>\u6216<code>food<\/code>\uff1b<code>(z\\|f)ood<\/code>\u5219\u5339\u914d<code>zood<\/code>\u6216<code>food<\/code><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\"><code>*<\/code><\/td>\n<td style=\"text-align: left;\">\u5339\u914d\u524d\u9762\u7684\u5b50\u8868\u8fbe\u5f0f\u96f6\u6b21\u6216\u591a\u6b21\u3002<br \/>\u4f8b\u5982\uff0c<code>zo*<\/code>\u80fd\u5339\u914d<code>z<\/code>\u4ee5\u53ca<code>zoo<\/code>\u3002<code>*<\/code>\u7b49\u4ef7\u4e8e<code>{0,}<\/code><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\"><code>+<\/code><\/td>\n<td style=\"text-align: left;\">\u5339\u914d\u524d\u9762\u7684\u5b50\u8868\u8fbe\u5f0f\u4e00\u6b21\u6216\u591a\u6b21\u3002<br \/>\u4f8b\u5982\uff0c<code>zo+<\/code>\u80fd\u5339\u914d<code>zo<\/code>\u4ee5\u53ca<code>zoo<\/code>\uff0c\u4f46\u4e0d\u80fd\u5339\u914d<code>z<\/code>\u3002+ \u7b49\u4ef7\u4e8e<code>{1,}<\/code><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\"><code>{n}<\/code><\/td>\n<td style=\"text-align: left;\"><code>n<\/code>\u662f\u4e00\u4e2a\u975e\u8d1f\u6574\u6570\u3002\u5339\u914d\u786e\u5b9a\u7684<code>n<\/code>\u6b21\u3002<br \/>\u4f8b\u5982\uff1a<code>o{2}<\/code>\u4e0d\u80fd\u5339\u914d<code>Bob<\/code>\u4e2d\u7684<code>o<\/code>\uff0c\u4f46\u662f\u80fd\u5339\u914d<code>food<\/code>\u4e2d\u7684\u4e24\u4e2a o<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\"><code>{n,m}<\/code><\/td>\n<td style=\"text-align: left;\"><code>m<\/code>\u548c<code>n<\/code>\u5747\u4e3a\u975e\u8d1f\u6574\u6570\uff0c\u5176\u4e2d<code>n &lt;= m<\/code>\u3002<br \/>\u6700\u5c11\u5339\u914d<code>n<\/code>\u6b21\u4e14\u6700\u591a\u5339\u914d<code>m<\/code>\u6b21<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>\u4f7f\u7528\u5b9e\u4f8b<\/h2>\n<p>\u4e86\u89e3\u4ee5\u4e0a\u7684\u6b63\u5219\u9700\u6c42\u540e\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u6839\u636e\u81ea\u5df1\u7684\u9700\u6c42\u6765\u7f16\u5199\u5e26\u6709\u6b63\u5219\u8868\u8fbe\u5f0f\u7684SQL\u8bed\u53e5\u3002\u4ee5\u4e0b\u5217\u51fa\u51e0\u4e2a\u5c0f\u5b9e\u4f8b(\u8868\u540d\uff1a<code>person_tbl<\/code>)\u6765\u52a0\u6df1\u6211\u4eec\u7684\u7406\u89e3\uff1a<\/p>\n<p>\u67e5\u627ename\u5b57\u6bb5\u4e2d\u4ee5&#8217;st&#8217;\u4e3a\u5f00\u5934\u7684\u6240\u6709\u6570\u636e\uff1a<\/p>\n<pre><code class=\"language-sql\">mysql&gt; SELECT name FROM person_tbl WHERE name REGEXP &#039;^st&#039;;<\/code><\/pre>\n<p>\u67e5\u627ename\u5b57\u6bb5\u4e2d\u4ee5&#8217;ok&#8217;\u4e3a\u7ed3\u5c3e\u7684\u6240\u6709\u6570\u636e\uff1a<\/p>\n<pre><code class=\"language-sql\">mysql&gt; SELECT name FROM person_tbl WHERE name REGEXP &#039;ok$&#039;;<\/code><\/pre>\n<p>\u67e5\u627ename\u5b57\u6bb5\u4e2d\u5305\u542b&#8217;mar&#8217;\u5b57\u7b26\u4e32\u7684\u6240\u6709\u6570\u636e\uff1a<\/p>\n<pre><code class=\"language-sql\">mysql&gt; SELECT name FROM person_tbl WHERE name REGEXP &#039;mar&#039;;<\/code><\/pre>\n<p>\u67e5\u627ename\u5b57\u6bb5\u4e2d\u4ee5\u5143\u97f3\u5b57\u7b26\u5f00\u5934\u6216\u4ee5&#8217;ok&#8217;\u5b57\u7b26\u4e32\u7ed3\u5c3e\u7684\u6240\u6709\u6570\u636e\uff1a<\/p>\n<pre><code class=\"language-sql\">mysql&gt; SELECT name FROM person_tbl WHERE name REGEXP &#039;^[aeiou]|ok$&#039;;<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u6211\u4eec\u5df2\u7ecf\u4e86\u89e3\u5230MySQL\u53ef\u4ee5\u901a\u8fc7LIKE &#8230;%\u6765\u8fdb\u884c\u6a21\u7cca\u5339\u914d\u3002 MySQL\u540c\u6837\u4e5f\u652f\u6301\u5176\u4ed6\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u5339\u914d\uff0c [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13],"tags":[],"class_list":["post-239","post","type-post","status-publish","format-standard","hentry","category-mysql"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/239","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=239"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/239\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=239"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=239"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=239"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}