{"id":471,"date":"2024-11-06T14:06:53","date_gmt":"2024-11-06T06:06:53","guid":{"rendered":"http:\/\/www.cmd137blog.top\/?p=471"},"modified":"2024-11-06T14:06:56","modified_gmt":"2024-11-06T06:06:56","slug":"java-%e6%ad%a3%e5%88%99%e8%a1%a8%e8%be%be%e5%bc%8f%e4%b8%8e%e7%88%ac%e8%99%ab","status":"publish","type":"post","link":"http:\/\/www.cmd137blog.top\/?p=471","title":{"rendered":"JAVA-\u6b63\u5219\u8868\u8fbe\u5f0f\u4e0e\u722c\u866b"},"content":{"rendered":"\n<h1 class=\"wp-block-heading\">1 \u6b63\u5219\u8868\u8fbe\u5f0f<\/h1>\n\n\n\n<p>\u4e0d\u7528\u8bb0\uff1a<a href=\"https:\/\/www.runoob.com\/java\/java-regular-expressions.html\">Java \u6b63\u5219\u8868\u8fbe\u5f0f | \u83dc\u9e1f\u6559\u7a0b<\/a><\/p>\n\n\n\n<p>\u793a\u4f8b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public static void main(String&#91;] args) {\n\n        \/\/\u5339\u914d\u8eab\u4efd\u8bc1\u53f7\n        \/\/350302 1985 07 04 018X\n        \/\/\u524d6\u4f4d\uff1a \u7b2c\u4e00\u4f4d\u4e0d\u80fd\u662f0\uff0c\u540e\u4e94\u4f4d\u968f\u673a     &#91;1-9]\\\\d{5}\n        \/\/\u5e74\u4efd\uff1a \u5f00\u593419|20 \u540e\u4e24\u4f4d\u968f\u673a         ((19)|(20))\\\\d{2}\n        \/\/\u6708\u4efd\uff1a01-09|10-12                ((0&#91;1-9])|(1&#91;0-2]))\n        \/\/\u65e5\u671f\uff1a01-09|10-19|20-29|30-31    ((0&#91;1-9])|(&#91;12]&#91;0-9])|(3&#91;01]))\n        \/\/\u540e\u56db\u4f4d\uff1a\u524d\u4e09\u4f4d\u968f\u673a\u6570\uff0c\u540e\u4e00\u4f4d\u6570\u5b57\u6216X\u6216x  \\\\d{3}&#91;\\\\dXx]\n        String regex=\"&#91;1-9]\\\\d{5}((19)|(20))\\\\d{2}((0&#91;1-9])|(1&#91;0-2]))((0&#91;1-9])|(&#91;12]&#91;0-9])|(3&#91;01]))\\\\d{3}&#91;\\\\dXx]\";\n\n        System.out.println(\"620902197501170729\".matches(regex));\/\/T\n        System.out.println(\"35030219850704018X\".matches(regex));\/\/T\n        System.out.println(\"42068319460201022x\".matches(regex));\/\/T\n        System.out.println(\"340304186603307579\".matches(regex));\/\/F\n        System.out.println(\"040426195606261248\".matches(regex));\/\/F\n        System.out.println(\"360483198213247775\".matches(regex));\/\/F\n    }<\/code><\/pre>\n\n\n\n<h1 class=\"wp-block-heading\">2 \u722c\u866b<\/h1>\n\n\n\n<p><em>\u6b64\u5904\u505a\u7b80\u5355\u5b66\u4e60\u5373\u53ef\uff0c\u5199\u722c\u866b\u7528Python\u4e0d\u9999\uff1f<\/em><\/p>\n\n\n\n<p><strong>java.util.regex<\/strong>&nbsp;\u5305\u662f Java \u6807\u51c6\u5e93\u4e2d\u7528\u4e8e\u652f\u6301\u6b63\u5219\u8868\u8fbe\u5f0f\u64cd\u4f5c\u7684\u5305\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Pattern \u7c7b\uff1a<\/strong>pattern \u5bf9\u8c61\u662f\u4e00\u4e2a\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u7f16\u8bd1\u8868\u793a\u3002Pattern \u7c7b\u6ca1\u6709\u516c\u5171\u6784\u9020\u65b9\u6cd5\u3002\u8981\u521b\u5efa\u4e00\u4e2a Pattern \u5bf9\u8c61\uff0c\u4f60\u5fc5\u987b\u9996\u5148\u8c03\u7528\u5176\u516c\u5171\u9759\u6001\u7f16\u8bd1\u65b9\u6cd5\uff0c\u5b83\u8fd4\u56de\u4e00\u4e2a Pattern \u5bf9\u8c61\u3002\u8be5\u65b9\u6cd5\u63a5\u53d7\u4e00\u4e2a\u6b63\u5219\u8868\u8fbe\u5f0f\u4f5c\u4e3a\u5b83\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u3002<\/li>\n\n\n\n<li><strong>Matcher \u7c7b\uff1a<\/strong>Matcher \u5bf9\u8c61\u662f\u5bf9\u8f93\u5165\u5b57\u7b26\u4e32\u8fdb\u884c\u89e3\u91ca\u548c\u5339\u914d\u64cd\u4f5c\u7684\u5f15\u64ce\u3002\u4e0ePattern \u7c7b\u4e00\u6837\uff0cMatcher \u4e5f\u6ca1\u6709\u516c\u5171\u6784\u9020\u65b9\u6cd5\u3002\u4f60\u9700\u8981\u8c03\u7528 Pattern \u5bf9\u8c61\u7684 matcher \u65b9\u6cd5\u6765\u83b7\u5f97\u4e00\u4e2a Matcher \u5bf9\u8c61\u3002<\/li>\n\n\n\n<li><strong>PatternSyntaxException\uff1a<\/strong>PatternSyntaxException \u662f\u4e00\u4e2a\u975e\u5f3a\u5236\u5f02\u5e38\u7c7b\uff0c\u5b83\u8868\u793a\u4e00\u4e2a\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f\u4e2d\u7684\u8bed\u6cd5\u9519\u8bef\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u793a\u4f8b1\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\n\nimport java.util.regex.Matcher;\nimport java.util.regex.Pattern;\n\npublic class Test {\n    public static void main(String&#91;] args) {\n        String content=\"JAVA \u662f\u4e00\u79cd\u5e7f\u6cdb\u4f7f\u7528\u7684\u7f16\u7a0b\u8bed\u8a00\uff0c\u7531Sun Microsystems\u4e8e1995\u5e74\u9996\u6b21\u53d1\u5e03\u3002\" +\n                \"\u968f\u7740\u6280\u672f\u7684\u53d1\u5c55\uff0cJava \u7ecf\u5386\u4e86\u591a\u4e2a\u7248\u672c\u7684\u66f4\u65b0\uff0c\u5305\u62ecJava 8\u4e8e2014\u5e743\u6708\u53d1\u5e03\uff0c\u4e3b\u8981\u5f15\u5165\u4e86Lambda\u8868\u8fbe\u5f0f\u3001Stream API\u548c\u65b0\u7684\u65f6\u95f4\u65e5\u671fAPI\uff1b\" +\n                \"Java 9\u4e8e2017\u5e749\u6708\u53d1\u5e03\uff0c\u6700\u663e\u8457\u7684\u7279\u6027\u662f\u6a21\u5757\u7cfb\u7edf\uff08Project Jigsaw\uff09\u548cJShell\uff0c\u589e\u5f3a\u4e86\u5f00\u53d1\u7684\u7075\u6d3b\u6027\uff1b\" +\n                \"Java 10\u4e8e2018\u5e743\u6708\u53d1\u5e03\uff0c\u52a0\u5165\u4e86\u5c40\u90e8\u53d8\u91cf\u7c7b\u578b\u63a8\u65ad\uff08var\uff09\u548c\u5e94\u7528\u7a0b\u5e8f\u7c7b\u6570\u636e\u5171\u4eab\uff1b\" +\n                \"Java 11\u4e8e2018\u5e749\u6708\u53d1\u5e03\uff0c\u662f\u4e00\u4e2a\u957f\u671f\u652f\u6301\u7248\u672c\uff08LTS\uff09\uff0c\u5f15\u5165\u4e86\u65b0\u7684API\u548c\u591a\u9879\u6027\u80fd\u589e\u5f3a\uff1b\";\n\n        \/\/\u83b7\u53d6\u6b63\u5219\u8868\u8fbe\u5f0f\u5bf9\u8c61\n        Pattern p = Pattern.compile(\"(?i)(JAVA) \\\\d{0,2}\");\n        \/\/\u83b7\u53d6\u6587\u672c\u5339\u914d\u5668\u5bf9\u8c61\n        Matcher m =p.matcher(content);\n\n        int count=0;\n        while (m.find()){\n            String s =m.group(0);\n            System.out.println(s);\n            count++;\n        }\n        System.out.println(\"count:\"+count);\n    }\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>1 \u6b63\u5219\u8868\u8fbe\u5f0f \u4e0d\u7528\u8bb0\uff1aJava \u6b63\u5219\u8868\u8fbe\u5f0f | \u83dc\u9e1f\u6559\u7a0b \u793a\u4f8b\uff1a 2 \u722c\u866b \u6b64\u5904\u505a\u7b80\u5355\u5b66\u4e60\u5373\u53ef\uff0c\u5199\u722c\u866b\u7528Py [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[56],"tags":[25],"class_list":["post-471","post","type-post","status-publish","format-standard","hentry","category-java","tag-java"],"_links":{"self":[{"href":"http:\/\/www.cmd137blog.top\/index.php?rest_route=\/wp\/v2\/posts\/471","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.cmd137blog.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.cmd137blog.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.cmd137blog.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.cmd137blog.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=471"}],"version-history":[{"count":2,"href":"http:\/\/www.cmd137blog.top\/index.php?rest_route=\/wp\/v2\/posts\/471\/revisions"}],"predecessor-version":[{"id":473,"href":"http:\/\/www.cmd137blog.top\/index.php?rest_route=\/wp\/v2\/posts\/471\/revisions\/473"}],"wp:attachment":[{"href":"http:\/\/www.cmd137blog.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=471"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.cmd137blog.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=471"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.cmd137blog.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=471"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}