{"id":311052,"date":"2024-05-20T22:05:34","date_gmt":"2024-05-20T14:05:34","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/311052.html"},"modified":"2024-05-20T22:05:55","modified_gmt":"2024-05-20T14:05:55","slug":"java-%e6%ad%a3%e5%88%99%e8%a1%a8%e8%be%be%e5%bc%8f-re-%e6%a8%a1%e5%9d%97%e6%80%8e%e4%b9%88%e4%bd%bf%e7%94%a8","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/311052.html","title":{"rendered":"java \u6b63\u5219\u8868\u8fbe\u5f0f re \u6a21\u5757\u600e\u4e48\u4f7f\u7528"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26231716\/73fc58e6-2b57-4156-ae91-cc91023a159f.webp\" alt=\"java \u6b63\u5219\u8868\u8fbe\u5f0f re \u6a21\u5757\u600e\u4e48\u4f7f\u7528\" \/><\/p>\n<p><p>Java\u4e2d\u5e76\u6ca1\u6709\u4e00\u4e2a\u540d\u4e3a<code>re<\/code>\u7684\u6a21\u5757\uff0c\u8fd9\u53ef\u80fd\u662f\u4e00\u4e2a\u5173\u4e8ePython\u7f16\u7a0b\u8bed\u8a00\u7684\u8bef\u89e3\uff0c\u56e0\u4e3a\u5728Python\u4e2d\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u529f\u80fd\u662f\u901a\u8fc7\u540d\u4e3a<code>re<\/code>\u7684\u6a21\u5757\u63d0\u4f9b\u7684\u3002\u4f46\u662f\u5728Java\u4e2d\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u529f\u80fd\u662f\u901a\u8fc7<code>java.util.regex<\/code>\u5305\u5b9e\u73b0\u7684\uff0c\u5176\u4e2d\u5305\u62ec<code>Pattern<\/code>\u548c<code>Matcher<\/code>\u7c7b\u7b49\u4e3b\u8981\u7ec4\u4ef6\u3002\u56e0\u6b64\uff0c\u672c\u6587\u5c06\u6307\u5bfc\u5982\u4f55\u5728Java\u4e2d\u4f7f\u7528<code>java.util.regex<\/code>\u5305\u8fdb\u884c\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001JAVA UTIL REGEX \u7b80\u4ecb<\/h3>\n<\/p>\n<p><p>\u6b63\u5219\u8868\u8fbe\u5f0f\u5bf9\u4e8e\u5904\u7406\u5b57\u7b26\u4e32\u975e\u5e38\u6709\u7528\uff0c<strong>Java\u4e2d\u901a\u8fc7<code>Pattern<\/code>\u548c<code>Matcher<\/code>\u7c7b\u6765\u5b9e\u73b0\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u529f\u80fd<\/strong>\uff0c\u5176\u4e2d<code>Pattern<\/code>\u5bf9\u8c61\u662f\u4e00\u4e2a\u7f16\u8bd1\u8fc7\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u800c<code>Matcher<\/code>\u5bf9\u8c61\u5219\u662f\u5bf9\u8f93\u5165\u5b57\u7b26\u4e32\u8fdb\u884c\u89e3\u6790\u548c\u5339\u914d\u64cd\u4f5c\u7684\u5f15\u64ce\u3002<\/p>\n<\/p>\n<p><h4>Pattern\u7c7b<\/h4>\n<\/p>\n<p><p>\u5728Java\u4e2d\uff0c\u8981\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u9996\u5148\u9700\u5c06\u5b57\u7b26\u4e32\u7f16\u8bd1\u6210<code>Pattern<\/code>\u5bf9\u8c61\u3002\u8fd9\u53ef\u4ee5\u901a\u8fc7\u8c03\u7528<code>Pattern.compile(String regex)<\/code>\u65b9\u6cd5\u5b8c\u6210\uff0c\u5176\u4e2d<code>regex<\/code>\u662f\u4f60\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u3002\u4e00\u4e2a<code>Pattern<\/code>\u5bf9\u8c61\u4ee3\u8868\u4e86\u7f16\u8bd1\u8fc7\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u6a21\u5f0f\u3002\u4e00\u65e6\u7f16\u8bd1\u5b8c\u6210\uff0c\u5c31\u53ef\u4ee5\u5c06\u8be5\u6a21\u5f0f\u7528\u4e8e\u521b\u5efa\u4e00\u4e2a<code>Matcher<\/code>\u5bf9\u8c61\uff0c\u8fdb\u800c\u5bf9\u5b57\u7b26\u4e32\u8fdb\u884c\u5339\u914d\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h4>Matcher\u7c7b<\/h4>\n<\/p>\n<p><p><code>Matcher<\/code>\u7c7b\u7528\u6765\u5bf9\u6587\u672c\u8fdb\u884c\u5339\u914d\u64cd\u4f5c\uff0c\u4f60\u9700\u8981\u4f7f\u7528<code>Pattern<\/code>\u5bf9\u8c61\u7684<code>matcher(CharSequence input)<\/code>\u65b9\u6cd5\u521b\u5efa\u51fa\u6765\u5b83\u3002<code>Matcher<\/code>\u5bf9\u8c61\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\uff0c\u5982<code>matches()<\/code>\u3001<code>find()<\/code>\u3001<code>group()<\/code>\u7b49\uff0c\u8fd9\u4e9b\u65b9\u6cd5\u7528\u4e8e\u6267\u884c\u4e0d\u540c\u7c7b\u578b\u7684\u641c\u7d22\u548c\u5339\u914d\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u521b\u5efaPATTERN\u548cMATCHER<\/h3>\n<\/p>\n<p><p>\u8981\u4f7f\u7528Java\u4e2d\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u9996\u5148\u9700\u8981\u521b\u5efa\u4e00\u4e2a<code>Pattern<\/code>\u5bf9\u8c61\uff0c\u7136\u540e\u7528\u5b83\u6765\u521b\u5efa\u4e00\u4e2a<code>Matcher<\/code>\u5bf9\u8c61\uff0c\u8fd9\u4e24\u6b65\u662f\u4f7f\u7528Java\u6b63\u5219\u8868\u8fbe\u5f0fAPI\u7684\u57fa\u672c\u6d41\u7a0b\u3002<\/p>\n<\/p>\n<p><h4>\u521b\u5efaPattern\u5bf9\u8c61<\/h4>\n<\/p>\n<p><pre><code class=\"language-java\">Pattern pattern = Pattern.compile(&quot;\u6b63\u5219\u8868\u8fbe\u5f0f&quot;);<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u884c\u4ee3\u7801\u4f1a\u5c06\u6307\u5b9a\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u7f16\u8bd1\u5e76\u8fd4\u56de\u4e00\u4e2a<code>Pattern<\/code>\u5bf9\u8c61\u3002\u7f16\u8bd1\u662f\u6307\u5c06\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u5b57\u7b26\u4e32\u5f62\u6001\u8f6c\u6362\u6210\u4e00\u4e2a\u53ef\u4ee5\u7528\u4e8e\u5339\u914d\u64cd\u4f5c\u7684\u6a21\u5f0f\u3002<\/p>\n<\/p>\n<p><h4>\u521b\u5efaMatcher\u5bf9\u8c61<\/h4>\n<\/p>\n<p><pre><code class=\"language-java\">Matcher matcher = pattern.matcher(&quot;\u9700\u8981\u5339\u914d\u7684\u5b57\u7b26\u4e32&quot;);<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7<code>Pattern<\/code>\u5bf9\u8c61\u7684<code>matcher<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a<code>Matcher<\/code>\u5bf9\u8c61\uff0c\u8fd9\u4e2a\u5bf9\u8c61\u53ef\u4ee5\u5bf9\u5b57\u7b26\u4e32\u6267\u884c\u5b9e\u9645\u7684\u5339\u914d\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u6267\u884c\u5339\u914d\u64cd\u4f5c<\/h3>\n<\/p>\n<p><p>\u521b\u5efa\u4e86<code>Pattern<\/code>\u548c<code>Matcher<\/code>\u5bf9\u8c61\u540e\uff0c\u5c31\u53ef\u4ee5\u6267\u884c\u5404\u79cd\u5339\u914d\u64cd\u4f5c\u4e86\u3002<\/p>\n<\/p>\n<p><h4>\u68c0\u67e5\u6574\u4e2a\u5b57\u7b26\u4e32\u662f\u5426\u5339\u914d<\/h4>\n<\/p>\n<p><pre><code class=\"language-java\">boolean matchFound = matcher.matches();<\/p>\n<p>if(matchFound) {<\/p>\n<p>    \/\/ \u5982\u679c\u6574\u4e2a\u5b57\u7b26\u4e32\u7b26\u5408\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8fd9\u4e2a\u8fc7\u7a0b\u68c0\u67e5\u6574\u4e2a\u76ee\u6807\u5b57\u7b26\u4e32\u662f\u5426\u7b26\u5408<code>Pattern<\/code>\u5bf9\u8c61\u6240\u8868\u793a\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f<\/strong>\u3002<\/p>\n<\/p>\n<p><h4>\u5bfb\u627e\u5339\u914d\u7684\u5b50\u5b57\u7b26\u4e32<\/h4>\n<\/p>\n<p><pre><code class=\"language-java\">while(matcher.find()) {<\/p>\n<p>    String match = matcher.group();<\/p>\n<p>    \/\/ \u6bcf\u6b21\u8c03\u7528find()\u65f6\uff0c\u5982\u679c\u627e\u5230\u4e00\u4e2a\u5339\u914d\u9879\uff0c\u5c31\u53ef\u4ee5\u901a\u8fc7group()\u65b9\u6cd5\u83b7\u53d6\u5339\u914d\u7684\u5b50\u5b57\u7b26\u4e32<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u5c55\u793a\u4e86\u5982\u4f55\u5bfb\u627e\u5b57\u7b26\u4e32\u4e2d\u6240\u6709\u4e0e\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u7684\u5b50\u4e32\uff0c\u6bcf\u6b21<code>find()<\/code>\u65b9\u6cd5\u627e\u5230\u4e00\u4e2a\u5339\u914d\u9879\u65f6\uff0c<code>group()<\/code>\u65b9\u6cd5\u4f1a\u8fd4\u56de\u5f53\u524d\u5339\u914d\u7684\u5b50\u4e32\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u793a\u4f8b<\/h3>\n<\/p>\n<p><p>\u8ba9\u6211\u4eec\u901a\u8fc7\u4e00\u4e9b\u5b9e\u9645\u7684\u4f8b\u5b50\u6765\u5c55\u793aJava\u4e2d\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u4f7f\u7528\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b1\uff1a\u9a8c\u8bc1\u7535\u5b50\u90ae\u4ef6\u5730\u5740<\/h4>\n<\/p>\n<p><pre><code class=\"language-java\">Pattern em<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>lPattern = Pattern.compile(&quot;^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\\\\.[a-zA-Z0-9-.]+$&quot;);<\/p>\n<p>Matcher emailMatcher = emailPattern.matcher(&quot;someone@example.com&quot;);<\/p>\n<p>boolean isEmailValid = emailMatcher.matches();<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u4f8b\u5b50\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u6821\u9a8c\u4e00\u4e2a\u7535\u5b50\u90ae\u4ef6\u5730\u5740\u662f\u5426\u6709\u6548\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b2\uff1a\u63d0\u53d6\u6570\u5b57<\/h4>\n<\/p>\n<p><pre><code class=\"language-java\">Pattern digitPattern = Pattern.compile(&quot;\\\\d+&quot;);<\/p>\n<p>Matcher digitMatcher = digitPattern.matcher(&quot;\u7535\u8bdd\u53f7\u7801: 12345&quot;);<\/p>\n<p>while(digitMatcher.find()) {<\/p>\n<p>    System.out.println(&quot;\u627e\u5230\u6570\u5b57: &quot; + digitMatcher.group());<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u4f8b\u5b50\u5c55\u793a\u4e86\u5982\u4f55\u63d0\u53d6\u5b57\u7b26\u4e32\u4e2d\u6240\u6709\u7684\u6570\u5b57\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u4e9b\u6b65\u9aa4\u548c\u4f8b\u5b50\uff0c\u4f60\u5e94\u8be5\u5bf9\u5982\u4f55\u5728Java\u4e2d\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6709\u4e86\u4e00\u4e2a\u57fa\u672c\u7684\u4e86\u89e3\u3002\u8bb0\u4f4f\uff0c\u5b9e\u8df5\u662f\u5b66\u4e60\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u6700\u597d\u65b9\u5f0f\uff0c\u5c1d\u8bd5\u66f4\u591a\u7684\u4f8b\u5b50\u5c06\u6709\u52a9\u4e8e\u4f60\u66f4\u597d\u5730\u638c\u63e1\u8fd9\u4e2a\u5f3a\u5927\u7684\u5de5\u5177\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1. \u600e\u6837\u5728Java\u4e2d\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u8fdb\u884c\u5339\u914d\uff1f<\/strong><\/p>\n<p>\u5728Java\u4e2d\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528<code>java.util.regex<\/code>\u5305\u4e2d\u7684<code>Pattern<\/code>\u548c<code>Matcher<\/code>\u7c7b\u6765\u8fdb\u884c\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u5339\u914d\u3002\u9996\u5148\uff0c\u4f7f\u7528<code>Pattern.compile()<\/code>\u65b9\u6cd5\u5c06\u6b63\u5219\u8868\u8fbe\u5f0f\u7f16\u8bd1\u4e3a\u4e00\u4e2a<code>Pattern<\/code>\u5bf9\u8c61\uff0c\u7136\u540e\u4f7f\u7528<code>Pattern.matcher()<\/code>\u65b9\u6cd5\u5c06\u5f85\u5339\u914d\u7684\u5b57\u7b26\u4e32\u4e0e<code>Pattern<\/code>\u5bf9\u8c61\u8fdb\u884c\u5339\u914d\u3002\u6700\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528<code>Matcher.matches()<\/code>\u65b9\u6cd5\u6765\u5224\u65ad\u662f\u5426\u5339\u914d\u6210\u529f\uff0c\u6216\u8005\u4f7f\u7528<code>Matcher.find()<\/code>\u65b9\u6cd5\u6765\u627e\u5230\u6240\u6709\u5339\u914d\u7684\u5b50\u4e32\u3002<\/p>\n<p><strong>2. \u5982\u4f55\u66ff\u6362\u5b57\u7b26\u4e32\u4e2d\u7684\u5339\u914d\u9879\uff1f<\/strong><\/p>\n<p>\u4f7f\u7528Java\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u53ef\u4ee5\u65b9\u4fbf\u5730\u5b9e\u73b0\u5b57\u7b26\u4e32\u7684\u66ff\u6362\u3002\u9996\u5148\uff0c\u4f7f\u7528<code>Pattern.compile()<\/code>\u65b9\u6cd5\u5c06\u6b63\u5219\u8868\u8fbe\u5f0f\u7f16\u8bd1\u4e3a\u4e00\u4e2a<code>Pattern<\/code>\u5bf9\u8c61\uff0c\u7136\u540e\u4f7f\u7528<code>Pattern.matcher()<\/code>\u65b9\u6cd5\u5c06\u5f85\u66ff\u6362\u7684\u5b57\u7b26\u4e32\u4e0e<code>Pattern<\/code>\u5bf9\u8c61\u8fdb\u884c\u5339\u914d\u3002\u63a5\u4e0b\u6765\uff0c\u53ef\u4ee5\u4f7f\u7528<code>Matcher.replaceAll()<\/code>\u65b9\u6cd5\u5c06\u5339\u914d\u5230\u7684\u5b50\u4e32\u66ff\u6362\u4e3a\u6307\u5b9a\u7684\u65b0\u5b57\u7b26\u4e32\u3002<\/p>\n<p><strong>3. \u662f\u5426\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u9a8c\u8bc1\u90ae\u7bb1\u5730\u5740\uff1f<\/strong><\/p>\n<p>\u662f\u7684\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528Java\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u9a8c\u8bc1\u90ae\u7bb1\u5730\u5740\u7684\u6709\u6548\u6027\u3002\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u9002\u5408\u90ae\u7bb1\u5730\u5740\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u7136\u540e\u4f7f\u7528<code>Pattern.compile()<\/code>\u65b9\u6cd5\u5c06\u5176\u7f16\u8bd1\u4e3a\u4e00\u4e2a<code>Pattern<\/code>\u5bf9\u8c61\u3002\u7136\u540e\uff0c\u4f7f\u7528<code>Pattern.matcher()<\/code>\u65b9\u6cd5\u5c06\u5f85\u9a8c\u8bc1\u7684\u90ae\u7bb1\u5730\u5740\u4e0e<code>Pattern<\/code>\u5bf9\u8c61\u8fdb\u884c\u5339\u914d\uff0c\u6700\u540e\u4f7f\u7528<code>Matcher.matches()<\/code>\u65b9\u6cd5\u6765\u5224\u65ad\u662f\u5426\u5339\u914d\u6210\u529f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Java\u4e2d\u5e76\u6ca1\u6709\u4e00\u4e2a\u540d\u4e3are\u7684\u6a21\u5757\uff0c\u8fd9\u53ef\u80fd\u662f\u4e00\u4e2a\u5173\u4e8ePython\u7f16\u7a0b\u8bed\u8a00\u7684\u8bef\u89e3\uff0c\u56e0\u4e3a\u5728Python\u4e2d\uff0c\u6b63\u5219\u8868\u8fbe [&hellip;]","protected":false},"author":3,"featured_media":311080,"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\/311052"}],"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=311052"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/311052\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/311080"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=311052"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=311052"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=311052"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}