{"id":2230,"date":"2023-05-06T22:03:57","date_gmt":"2023-05-06T14:03:57","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=2230"},"modified":"2023-05-06T22:04:05","modified_gmt":"2023-05-06T14:04:05","slug":"groovy-code-example-regular-expression","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/05\/06\/groovy-code-example-regular-expression\/","title":{"rendered":"Groovy\u4ee3\u7801\u793a\u4f8b &#8211; \u6b63\u5219\u8868\u8fbe\u5f0f"},"content":{"rendered":"<h2>\u6b63\u5219\u8868\u8fbe\u5f0f<\/h2>\n<p>\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u4e00\u79cd\u641c\u7d22\u6a21\u5f0f\uff0c\u5c24\u5176\u9002\u7528\u4e8e\u5b57\u7b26\u4e32\u7684\u641c\u7d22\u3002\u5728\u5176\u4ed6\u8bed\u8a00\u4e2d\uff0c\u4f60\u53ef\u80fd\u89c1\u5230\u8fc7Regex\u6216\u8005Regexp\u4ee3\u8868\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u63a5\u4e0b\u6765\u6211\u4eec\u5c06\u6f14\u793agroovy\u4e2d\u7684\u6b63\u5219\u662f\u5982\u4f55\u7684\u7b80\u5355\u9002\u7528\u3002<\/p>\n<p>Groovy\u652f\u6301\u901a\u8fc7<code>~<\/code>\u7b26\u53f7\u5b9a\u4e49\u6b63\u5219\u3002\u5f53\u4f60\u4f7f\u7528<code>~<\/code>\u5b9a\u4e49\u6b63\u5219\u7684\u65f6\u5019\uff0c\u76f8\u5f53\u4e8ejava\u4e2d\u7684<code>java.util.regex.Pattern<\/code>\u5bf9\u8c61\u3002\u8fd9\u610f\u5473\u7740\u4f60\u53ef\u4ee5\u50cfjava\u4ee3\u7801\u4e2d\u90a3\u6837\u53bb\u4f7f\u7528\u5b83\u3002\u6bd4\u5982\uff1a<\/p>\n<pre><code class=\"language-java\">\/\/\u4f7f\u7528~\u5b9a\u4e49\u4e00\u4e2aIP\u5730\u5740\u7684\u6b63\u5219\ndef ipAddress = ~\/([0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+)\/\n\n\/\/\u663e\u793a\u6b63\u5219\u5bf9\u8c61\u7684\u7c7b\u540d\nprintln ipAddress.class.name \/\/ java.util.regex.Pattern<\/code><\/pre>\n<pre><code class=\"language-java\">\/\/\u5b9a\u4e49\u4e00\u4e2a\u6b63\u5219\ndef nameRegex = ~&#039;john&#039;\n\/\/\u5c1d\u8bd5\u4e0e&quot;john&quot;\u5b57\u7b26\u4e32\u5339\u914d\nprintln nameRegex.matcher(&quot;john&quot;).matches() \/\/ true\n\n\/\/\u5b9a\u4e49IP\u5730\u5740\u7684\u6b63\u5219\ndef ipAddressRegex = ~\/([0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+)\/\n\/\/\u5c1d\u8bd5\u4e0e127.0.0.1\u8fd9\u4e2aIP\u5730\u5740\u5339\u914d\nprintln ipAddressRegex.matcher(&quot;127.0.0.1&quot;).matches() \/\/ true<\/code><\/pre>\n<h2>\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u9ad8\u7ea7\u5e94\u7528<\/h2>\n<p>Groovy\u53ef\u4ee5\u5f88\u65b9\u4fbf\u7684\u521b\u5efa\u6a21\u5f0f\u5e76\u5339\u914d\u3002\u4f8b\u5982\uff0c\u5047\u5982\u4f60\u9700\u8981\u5224\u65ad\u4e00\u4e2a\u5b57\u7b26\u4e32\u662f\u4e0d\u662f\u4ee5&#8217;L&#8217;\u5f00\u5934\u5e76\u4ee5&#8217;s&#8217;\u7ed3\u5c3e\uff0c\u4f60\u53ef\u4ee5\u8fd9\u6837\u4f7f\u7528\uff1a<\/p>\n<pre><code class=\"language-java\">\/\/\u5b9a\u4e49\u6b63\u5219\uff0cL\u5f00\u5934\u5e76\u4ee5e\u7ed3\u5c3e\ndef pattern = ~&#039;L....e&#039;\n\n\/\/\u5c1d\u8bd5\u5339\u914d\nprintln pattern.matcher(&quot;Little&quot;).matches() \/\/ true<\/code><\/pre>\n<p>\u4f60\u4e5f\u53ef\u4ee5\u4f7f\u7528<code>isCase()<\/code>\u65b9\u6cd5\u5224\u65ad\u6a21\u5f0f\u662f\u5426\u5339\u914d\u3002\u6bd4\u5982\uff0c\u4f60\u53ea\u77e5\u9053\u5b57\u7b26\u4e32\u7684\u5f00\u59cb\u548c\u7ed3\u675f\u5b57\u7b26\uff0c\u4f60\u60f3\u68c0\u67e5\u8fd9\u4e2a\u5b57\u7b26\u4e32\u662f\u4e0d\u662f\u4e0e\u6b63\u5219\u8868\u793a\u5f0f\u76f8\u5339\u914d\uff0c\u4f60\u53ef\u4ee5\u8fd9\u6837\u4f7f\u7528\uff1a<\/p>\n<pre><code class=\"language-java\">\/\/\u5b9a\u4e49\u6b63\u5219\u8868\u8fbe\u5f0f\ndef isCasePattern = ~\/L\\w+e\/\n\/\/\u5224\u65ad\u662f\u5426\u5339\u914d\nprintln isCasePattern.isCase(&quot;Little&quot;)<\/code><\/pre>\n<pre><code class=\"language-java\">\/\/\u5b9a\u4e49\u4e00\u4e2a\u6b63\u5219\uff1aA\u5f00\u5934\u548c\u5b57\u6bcd\u7ed3\u5c3e\ndef grepPattern = ~\/A\\w+\/\n\/\/\u5b9a\u4e49\u4e00\u4e2a\u5217\u8868\ndef cities = [&#039;Alabama&#039;, &#039;Los Angeles&#039;, &#039;Arizona&#039;]\n\/\/\u5728list\u4e2d\u6309\u7167\u6b63\u5219\u8868\u8fbe\u5f0f\u67e5\u627e\nprintln cities.grep(grepPattern) \/\/ [Alabama, Arizona]<\/code><\/pre>\n<p>\u4e0a\u9762\u7684\u5217\u8868\u4e2d\uff0cAlabama\u548cArizona\u90fd\u80fd\u6ee1\u8db3\u4ee5A\u5f00\u5934\u548c\u5b57\u6bcd\u7ed3\u5c3e\u7684\u6b63\u5219\u8868\u793a\u5f0f\uff0c\u56e0\u6b64\u88ab\u67e5\u627e\u51fa\u6765<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6b63\u5219\u8868\u8fbe\u5f0f \u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u4e00\u79cd\u641c\u7d22\u6a21\u5f0f\uff0c\u5c24\u5176\u9002\u7528\u4e8e\u5b57\u7b26\u4e32\u7684\u641c\u7d22\u3002\u5728\u5176\u4ed6\u8bed\u8a00\u4e2d\uff0c\u4f60\u53ef\u80fd\u89c1\u5230\u8fc7Regex\u6216\u8005Rege [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[568],"tags":[],"class_list":["post-2230","post","type-post","status-publish","format-standard","hentry","category-groovy"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/2230","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=2230"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/2230\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=2230"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=2230"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=2230"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}