{"id":2214,"date":"2025-03-02T10:33:55","date_gmt":"2025-03-02T02:33:55","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=2214"},"modified":"2025-03-02T10:33:55","modified_gmt":"2025-03-02T02:33:55","slug":"java-%e6%ad%a3%e5%88%99%e8%a1%a8%e8%be%be%e5%bc%8f","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/03\/02\/java-%e6%ad%a3%e5%88%99%e8%a1%a8%e8%be%be%e5%bc%8f\/","title":{"rendered":"Java \u6b63\u5219\u8868\u8fbe\u5f0f"},"content":{"rendered":"\n<p>Java \u6b63\u5219\u8868\u8fbe\u5f0f\uff08Regex\uff0cRegular Expression\uff09\u662f\u7528\u6765\u63cf\u8ff0\u5b57\u7b26\u4e32\u6a21\u5f0f\u7684\u5de5\u5177\uff0c\u5b83\u5141\u8bb8\u4f60\u68c0\u67e5\u4e00\u4e2a\u5b57\u7b26\u4e32\u662f\u5426\u7b26\u5408\u67d0\u79cd\u6a21\u5f0f\uff0c\u6216\u8005\u901a\u8fc7\u67d0\u4e9b\u89c4\u5219\u5bf9\u5b57\u7b26\u4e32\u8fdb\u884c\u5339\u914d\u3001\u66ff\u6362\u3001\u5206\u5272\u7b49\u64cd\u4f5c\u3002Java \u4e2d\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u7531 <code>java.util.regex<\/code> \u5305\u63d0\u4f9b\uff0c\u5305\u62ec <code>Pattern<\/code> \u548c <code>Matcher<\/code> \u7c7b\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1. \u6b63\u5219\u8868\u8fbe\u5f0f\u57fa\u7840<\/strong><\/h3>\n\n\n\n<p>\u6b63\u5219\u8868\u8fbe\u5f0f\u7531\u5b57\u7b26\u3001\u5143\u5b57\u7b26\uff08Metacharacters\uff09\u4ee5\u53ca\u7279\u6b8a\u7684\u89c4\u5219\u7ec4\u5408\u800c\u6210\u3002\u5e38\u7528\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u5143\u5b57\u7b26\u5305\u62ec\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>.<\/code>\uff1a\u5339\u914d\u4efb\u610f\u5355\u4e2a\u5b57\u7b26\uff08\u9664\u6362\u884c\u7b26\u4e4b\u5916\uff09\u3002<\/li>\n\n\n\n<li><code>^<\/code>\uff1a\u5339\u914d\u8f93\u5165\u5b57\u7b26\u4e32\u7684\u5f00\u59cb\u3002<\/li>\n\n\n\n<li><code>$<\/code>\uff1a\u5339\u914d\u8f93\u5165\u5b57\u7b26\u4e32\u7684\u7ed3\u675f\u3002<\/li>\n\n\n\n<li><code>*<\/code>\uff1a\u5339\u914d\u524d\u9762\u7684\u5b50\u8868\u8fbe\u5f0f\u96f6\u6b21\u6216\u591a\u6b21\u3002<\/li>\n\n\n\n<li><code>+<\/code>\uff1a\u5339\u914d\u524d\u9762\u7684\u5b50\u8868\u8fbe\u5f0f\u4e00\u6b21\u6216\u591a\u6b21\u3002<\/li>\n\n\n\n<li><code>?<\/code>\uff1a\u5339\u914d\u524d\u9762\u7684\u5b50\u8868\u8fbe\u5f0f\u96f6\u6b21\u6216\u4e00\u6b21\u3002<\/li>\n\n\n\n<li><code>{n}<\/code>\uff1a\u5339\u914d\u524d\u9762\u7684\u5b50\u8868\u8fbe\u5f0f\u6070\u597d n \u6b21\u3002<\/li>\n\n\n\n<li><code>{n,}<\/code>\uff1a\u5339\u914d\u524d\u9762\u7684\u5b50\u8868\u8fbe\u5f0f\u81f3\u5c11 n \u6b21\u3002<\/li>\n\n\n\n<li><code>{n,m}<\/code>\uff1a\u5339\u914d\u524d\u9762\u7684\u5b50\u8868\u8fbe\u5f0f\u81f3\u5c11 n \u6b21\uff0c\u81f3\u591a m \u6b21\u3002<\/li>\n\n\n\n<li><code>[]<\/code>\uff1a\u5339\u914d\u62ec\u53f7\u5185\u7684\u4efb\u610f\u5b57\u7b26\u3002<\/li>\n\n\n\n<li><code>|<\/code>\uff1a\u8868\u793a\u903b\u8f91\u201c\u6216\u201d\u3002<\/li>\n\n\n\n<li><code>()<\/code>\uff1a\u5206\u7ec4\uff0c\u7528\u4e8e\u5c06\u591a\u4e2a\u5b57\u7b26\u6216\u8868\u8fbe\u5f0f\u4f5c\u4e3a\u4e00\u4e2a\u5355\u5143\u5904\u7406\u3002<\/li>\n\n\n\n<li><code>\\d<\/code>\uff1a\u5339\u914d\u4e00\u4e2a\u6570\u5b57\u5b57\u7b26\uff0c\u76f8\u5f53\u4e8e <code>[0-9]<\/code>\u3002<\/li>\n\n\n\n<li><code>\\D<\/code>\uff1a\u5339\u914d\u4e00\u4e2a\u975e\u6570\u5b57\u5b57\u7b26\uff0c\u76f8\u5f53\u4e8e <code>[^0-9]<\/code>\u3002<\/li>\n\n\n\n<li><code>\\w<\/code>\uff1a\u5339\u914d\u4e00\u4e2a\u5b57\u6bcd\u3001\u6570\u5b57\u6216\u4e0b\u5212\u7ebf\u5b57\u7b26\uff0c\u76f8\u5f53\u4e8e <code>[a-zA-Z0-9_]<\/code>\u3002<\/li>\n\n\n\n<li><code>\\W<\/code>\uff1a\u5339\u914d\u4e00\u4e2a\u975e\u5b57\u6bcd\u3001\u975e\u6570\u5b57\u6216\u975e\u4e0b\u5212\u7ebf\u5b57\u7b26\uff0c\u76f8\u5f53\u4e8e <code>[^a-zA-Z0-9_]<\/code>\u3002<\/li>\n\n\n\n<li><code>\\s<\/code>\uff1a\u5339\u914d\u4efb\u4f55\u7a7a\u767d\u5b57\u7b26\uff08\u7a7a\u683c\u3001\u5236\u8868\u7b26\u3001\u6362\u884c\u7b26\u7b49\uff09\u3002<\/li>\n\n\n\n<li><code>\\S<\/code>\uff1a\u5339\u914d\u4efb\u4f55\u975e\u7a7a\u767d\u5b57\u7b26\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2. <code>Pattern<\/code> \u7c7b<\/strong><\/h3>\n\n\n\n<p><code>Pattern<\/code> \u7c7b\u662f Java \u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u6838\u5fc3\uff0c\u5b83\u662f\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u62bd\u8c61\u8868\u793a\u3002\u6b63\u5219\u8868\u8fbe\u5f0f\u5b57\u7b26\u4e32\u4f1a\u88ab\u7f16\u8bd1\u6210\u4e00\u4e2a <code>Pattern<\/code> \u5bf9\u8c61\uff0c\u5e76\u4e14\u53ef\u4ee5\u88ab\u591a\u4e2a <code>Matcher<\/code> \u5bf9\u8c61\u91cd\u7528\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">2.1 \u521b\u5efa <code>Pattern<\/code> \u5bf9\u8c61<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.regex.*;\n\nPattern pattern = Pattern.compile(\"a*b\");  \/\/ \u7f16\u8bd1\u6b63\u5219\u8868\u8fbe\u5f0f<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">2.2 <code>Pattern<\/code> \u7c7b\u7684\u5e38\u7528\u65b9\u6cd5<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>compile(String regex)<\/code>\uff1a\u7f16\u8bd1\u6b63\u5219\u8868\u8fbe\u5f0f\u5e76\u8fd4\u56de <code>Pattern<\/code> \u5bf9\u8c61\u3002<\/li>\n\n\n\n<li><code>matcher(CharSequence input)<\/code>\uff1a\u8fd4\u56de\u4e00\u4e2a <code>Matcher<\/code> \u5bf9\u8c61\uff0c\u7528\u4e8e\u5bf9\u8f93\u5165\u5b57\u7b26\u4e32\u8fdb\u884c\u5339\u914d\u3002<\/li>\n\n\n\n<li><code>split(CharSequence input)<\/code>\uff1a\u5c06\u5b57\u7b26\u4e32\u6309\u6b63\u5219\u8868\u8fbe\u5f0f\u5206\u5272\u3002<\/li>\n\n\n\n<li><code>matches()<\/code>\uff1a\u5224\u65ad\u5b57\u7b26\u4e32\u662f\u5426\u5b8c\u5168\u7b26\u5408\u6b63\u5219\u8868\u8fbe\u5f0f\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3. <code>Matcher<\/code> \u7c7b<\/strong><\/h3>\n\n\n\n<p><code>Matcher<\/code> \u7c7b\u63d0\u4f9b\u4e86\u5bf9\u5b57\u7b26\u4e32\u8fdb\u884c\u6b63\u5219\u5339\u914d\u7684\u529f\u80fd\u3002\u901a\u8fc7 <code>Pattern<\/code> \u7c7b\u751f\u6210 <code>Matcher<\/code> \u5bf9\u8c61\u540e\uff0c\u53ef\u4ee5\u8c03\u7528 <code>Matcher<\/code> \u7c7b\u63d0\u4f9b\u7684\u65b9\u6cd5\u6765\u6267\u884c\u5339\u914d\u3001\u67e5\u627e\u3001\u66ff\u6362\u7b49\u64cd\u4f5c\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">3.1 \u521b\u5efa <code>Matcher<\/code> \u5bf9\u8c61<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>Pattern pattern = Pattern.compile(\"a*b\");\nMatcher matcher = pattern.matcher(\"aaab\");<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">3.2 <code>Matcher<\/code> \u7c7b\u7684\u5e38\u7528\u65b9\u6cd5<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>find()<\/code>\uff1a\u67e5\u627e\u662f\u5426\u5b58\u5728\u5339\u914d\u9879\u3002\u5982\u679c\u5b58\u5728\uff0c\u8fd4\u56de <code>true<\/code>\uff0c\u5426\u5219\u8fd4\u56de <code>false<\/code>\u3002<\/li>\n\n\n\n<li><code>matches()<\/code>\uff1a\u5c1d\u8bd5\u5339\u914d\u6574\u4e2a\u5b57\u7b26\u4e32\uff0c\u5982\u679c\u5b57\u7b26\u4e32\u5b8c\u5168\u7b26\u5408\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u5219\u8fd4\u56de <code>true<\/code>\uff0c\u5426\u5219\u8fd4\u56de <code>false<\/code>\u3002<\/li>\n\n\n\n<li><code>group()<\/code>\uff1a\u8fd4\u56de\u5f53\u524d\u5339\u914d\u9879\u7684\u5b50\u5b57\u7b26\u4e32\u3002<\/li>\n\n\n\n<li><code>start()<\/code>\uff1a\u8fd4\u56de\u5339\u914d\u9879\u5728\u539f\u5b57\u7b26\u4e32\u4e2d\u7684\u5f00\u59cb\u4f4d\u7f6e\u3002<\/li>\n\n\n\n<li><code>end()<\/code>\uff1a\u8fd4\u56de\u5339\u914d\u9879\u5728\u539f\u5b57\u7b26\u4e32\u4e2d\u7684\u7ed3\u675f\u4f4d\u7f6e\u3002<\/li>\n\n\n\n<li><code>replaceAll(String replacement)<\/code>\uff1a\u7528\u6307\u5b9a\u7684\u5b57\u7b26\u4e32\u66ff\u6362\u6240\u6709\u5339\u914d\u7684\u5b50\u4e32\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4. \u6b63\u5219\u8868\u8fbe\u5f0f\u793a\u4f8b<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">4.1 \u5b57\u7b26\u4e32\u5339\u914d<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.regex.*;\n\npublic class RegexExample {\n    public static void main(String&#91;] args) {\n        \/\/ \u7f16\u8bd1\u6b63\u5219\u8868\u8fbe\u5f0f\n        Pattern pattern = Pattern.compile(\"a*b\");\n        Matcher matcher = pattern.matcher(\"aaab\");\n\n        \/\/ \u67e5\u627e\u5339\u914d\u9879\n        if (matcher.matches()) {\n            System.out.println(\"\u5b57\u7b26\u4e32\u5b8c\u5168\u5339\u914d\");\n        } else {\n            System.out.println(\"\u5b57\u7b26\u4e32\u4e0d\u5339\u914d\");\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<p><strong>\u8f93\u51fa<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u5b57\u7b26\u4e32\u5b8c\u5168\u5339\u914d<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">4.2 \u67e5\u627e\u5339\u914d\u9879<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.regex.*;\n\npublic class RegexExample {\n    public static void main(String&#91;] args) {\n        Pattern pattern = Pattern.compile(\"\\\\d+\");  \/\/ \u5339\u914d\u4e00\u4e2a\u6216\u591a\u4e2a\u6570\u5b57\n        Matcher matcher = pattern.matcher(\"abc123xyz456\");\n\n        \/\/ \u67e5\u627e\u5339\u914d\u9879\n        while (matcher.find()) {\n            System.out.println(\"\u627e\u5230\u5339\u914d\u9879: \" + matcher.group());\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<p><strong>\u8f93\u51fa<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u627e\u5230\u5339\u914d\u9879: 123\n\u627e\u5230\u5339\u914d\u9879: 456<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">4.3 \u4f7f\u7528\u5206\u7ec4<\/h4>\n\n\n\n<p>\u6b63\u5219\u8868\u8fbe\u5f0f\u4e2d\u7684\u5206\u7ec4\uff08<code>()<\/code>\uff09\u53ef\u4ee5\u63d0\u53d6\u5b57\u7b26\u4e32\u7684\u4e00\u90e8\u5206\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.regex.*;\n\npublic class RegexExample {\n    public static void main(String&#91;] args) {\n        Pattern pattern = Pattern.compile(\"(\\\\d+)-(\\\\d+)\");\n        Matcher matcher = pattern.matcher(\"123-456\");\n\n        if (matcher.find()) {\n            System.out.println(\"\u7b2c\u4e00\u4e2a\u5206\u7ec4: \" + matcher.group(1));  \/\/ \u8f93\u51fa 123\n            System.out.println(\"\u7b2c\u4e8c\u4e2a\u5206\u7ec4: \" + matcher.group(2));  \/\/ \u8f93\u51fa 456\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<p><strong>\u8f93\u51fa<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u7b2c\u4e00\u4e2a\u5206\u7ec4: 123\n\u7b2c\u4e8c\u4e2a\u5206\u7ec4: 456<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">4.4 \u66ff\u6362\u5339\u914d\u9879<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.regex.*;\n\npublic class RegexExample {\n    public static void main(String&#91;] args) {\n        Pattern pattern = Pattern.compile(\"\\\\d+\");  \/\/ \u5339\u914d\u6570\u5b57\n        Matcher matcher = pattern.matcher(\"abc123xyz456\");\n\n        \/\/ \u7528 \"#\" \u66ff\u6362\u6240\u6709\u7684\u6570\u5b57\n        String result = matcher.replaceAll(\"#\");\n        System.out.println(result);  \/\/ \u8f93\u51fa abc#xyz#\n    }\n}<\/code><\/pre>\n\n\n\n<p><strong>\u8f93\u51fa<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>abc#xyz#<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">4.5 \u5206\u5272\u5b57\u7b26\u4e32<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.regex.*;\n\npublic class RegexExample {\n    public static void main(String&#91;] args) {\n        String str = \"apple,banana,orange\";\n        String&#91;] fruits = str.split(\",\");\n\n        for (String fruit : fruits) {\n            System.out.println(fruit);\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<p><strong>\u8f93\u51fa<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>apple\nbanana\norange<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>5. \u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u5e38\u7528\u5e94\u7528<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">5.1 \u9a8c\u8bc1\u90ae\u7bb1<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.regex.*;\n\npublic class RegexExample {\n    public static void main(String&#91;] args) {\n        String email = \"test@example.com\";\n        String regex = \"^&#91;a-zA-Z0-9_+&amp;*-]+(?:\\\\.&#91;a-zA-Z0-9_+&amp;*-]+)*@(?:&#91;a-zA-Z0-9-]+\\\\.)+&#91;a-zA-Z]{2,7}$\";\n        Pattern pattern = Pattern.compile(regex);\n        Matcher matcher = pattern.matcher(email);\n\n        if (matcher.matches()) {\n            System.out.println(\"\u90ae\u7bb1\u683c\u5f0f\u6b63\u786e\");\n        } else {\n            System.out.println(\"\u90ae\u7bb1\u683c\u5f0f\u4e0d\u6b63\u786e\");\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<p><strong>\u8f93\u51fa<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u90ae\u7bb1\u683c\u5f0f\u6b63\u786e<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">5.2 \u9a8c\u8bc1\u624b\u673a\u53f7\uff08\u4ee5\u4e2d\u56fd\u4e3a\u4f8b\uff09<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.regex.*;\n\npublic class RegexExample {\n    public static void main(String&#91;] args) {\n        String phone = \"13812345678\";\n        String regex = \"^(1&#91;3-9])\\\\d{9}$\";\n        Pattern pattern = Pattern.compile(regex);\n        Matcher matcher = pattern.matcher(phone);\n\n        if (matcher.matches()) {\n            System.out.println(\"\u624b\u673a\u53f7\u683c\u5f0f\u6b63\u786e\");\n        } else {\n            System.out.println(\"\u624b\u673a\u53f7\u683c\u5f0f\u4e0d\u6b63\u786e\");\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<p><strong>\u8f93\u51fa<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u624b\u673a\u53f7\u683c\u5f0f\u6b63\u786e<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">5.3 \u9a8c\u8bc1\u5bc6\u7801\uff08\u8981\u6c42\u81f3\u5c118\u4e2a\u5b57\u7b26\uff0c\u5305\u62ec\u5927\u5c0f\u5199\u5b57\u6bcd\u548c\u6570\u5b57\uff09<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.regex.*;\n\npublic class RegexExample {\n    public static void main(String&#91;] args) {\n        String password = \"Password123\";\n        String regex = \"^(?=.*&#91;A-Z])(?=.*&#91;a-z])(?=.*\\\\d).{8,}$\";\n        Pattern pattern = Pattern.compile(regex);\n        Matcher matcher = pattern.matcher(password);\n\n        if (matcher.matches()) {\n            System.out.println(\"\u5bc6\u7801\u683c\u5f0f\u6b63\u786e\");\n        } else {\n            System.out.println(\"\u5bc6\u7801\u683c\u5f0f\u4e0d\u6b63\u786e\");\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<p><strong>\u8f93\u51fa<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u5bc6\u7801\u683c\u5f0f\u6b63\u786e<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>6. \u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u6027\u80fd\u4f18\u5316<\/strong><\/h3>\n\n\n\n<p>\u5728 Java \u4e2d\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u65f6\uff0c\u901a\u5e38\u4f1a\u6d89\u53ca\u6027\u80fd\u65b9\u9762\u7684\u95ee\u9898\uff0c\u5c24\u5176\u662f\u5f53\u6b63\u5219\u8868\u8fbe\u5f0f\u8f83\u4e3a\u590d\u6742\u65f6\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u6027\u80fd\u4f18\u5316\u7684\u5efa\u8bae\uff1a<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">6.1 \u4f7f\u7528\u9884\u7f16\u8bd1\u7684\u6b63\u5219\u8868\u8fbe\u5f0f<\/h4>\n\n\n\n<p>\u5982\u679c\u4f60\u5728\u591a\u4e2a\u5730\u65b9\u4f7f\u7528\u76f8\u540c\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u63a8\u8350\u4f7f\u7528 <code>Pattern.compile()<\/code> \u65b9\u6cd5\u5c06\u5176\u9884\u7f16\u8bd1\u4e3a <code>Pattern<\/code> \u5bf9\u8c61\uff0c\u8fd9\u6837\u53ef\u4ee5\u907f\u514d\u6bcf\u6b21\u4f7f\u7528\u65f6\u91cd\u590d\u7f16\u8bd1\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u4ece\u800c\u63d0\u9ad8\u6027\u80fd\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Pattern pattern = Pattern.compile(\"\\\\d+\");  \/\/ \u9884\u7f16\u8bd1\u6b63\u5219\u8868\u8fbe\u5f0f\nMatcher matcher = pattern.matcher(\"1234\");<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">6.2 \u4f7f\u7528\u975e\u8d2a\u5a6a\u6a21\u5f0f<\/h4>\n\n\n\n<p>\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u8d2a\u5a6a\u7684\uff0c\u5373\u5c3d\u53ef\u80fd\u5339\u914d\u66f4\u591a\u7684\u5b57\u7b26\u3002\u6709\u65f6\u4f7f\u7528\u975e\u8d2a\u5a6a\u6a21\u5f0f\uff08\u5373\u201c\u61d2\u60f0\u6a21\u5f0f\u201d\uff09\u53ef\u4ee5\u63d0\u9ad8\u6548\u7387\uff0c\u5c24\u5176\u662f\u5728\u5904\u7406\u5927\u91cf\u6570\u636e\u65f6\u3002\u975e\u8d2a\u5a6a\u6a21\u5f0f\u4f7f\u7528 <code>*?<\/code>\u3001<code>+?<\/code>\u3001<code>??<\/code> \u7b49\u3002<\/p>\n\n\n\n<p>\u4f8b\u5982\uff0c<code>\".*\"<\/code> \u662f\u8d2a\u5a6a\u6a21\u5f0f\uff0c\u4f1a\u5c3d\u53ef\u80fd\u591a\u5730\u5339\u914d\u5b57\u7b26\uff0c\u800c <code>\".*?\"<\/code> \u662f\u975e\u8d2a\u5a6a\u6a21\u5f0f\uff0c\u4f1a\u5c3d\u91cf\u5c11\u5339\u914d\u5b57\u7b26\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Pattern pattern = Pattern.compile(\"&lt;.*?&gt;\");  \/\/ \u975e\u8d2a\u5a6a\u6a21\u5f0f<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">6.3 \u907f\u514d\u590d\u6742\u7684\u6b63\u5219\u8868\u8fbe\u5f0f<\/h4>\n\n\n\n<p>\u4e00\u4e9b\u590d\u6742\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u53ef\u80fd\u4f1a\u5bfc\u81f4\u6027\u80fd\u95ee\u9898\u3002\u5c3d\u91cf\u907f\u514d\u8fc7\u591a\u7684\u5206\u7ec4\u3001\u56de\u6eaf\u548c\u4f7f\u7528\u4e0d\u5fc5\u8981\u7684\u9ad8\u7ea7\u529f\u80fd\u3002\u5c3d\u91cf\u7b80\u5316\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u5e76\u5728\u9700\u8981\u65f6\u9010\u6b65\u4f18\u5316\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">6.4 \u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u65f6\u907f\u514d\u8fc7\u591a\u4f7f\u7528 <code>find()<\/code> \u65b9\u6cd5<\/h4>\n\n\n\n<p>\u5728\u591a\u6b21\u6267\u884c <code>find()<\/code> \u65b9\u6cd5\u65f6\uff0c\u5982\u679c\u6a21\u5f0f\u662f <code>find()<\/code> \u8c03\u7528\u7684\u6761\u4ef6\uff0c\u6bcf\u6b21\u8c03\u7528\u90fd\u4f1a\u4f7f\u5f97\u6b63\u5219\u8868\u8fbe\u5f0f\u5f15\u64ce\u8fdb\u884c\u591a\u6b21\u8ba1\u7b97\u3002\u56e0\u6b64\uff0c\u5f53\u4f60\u77e5\u9053\u9700\u8981\u591a\u6b21\u67e5\u627e\u65f6\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u4e00\u6b21 <code>find()<\/code> \u6765\u5339\u914d\u6240\u6709\u7ed3\u679c\uff0c\u51cf\u5c11\u591a\u6b21\u5339\u914d\u8fc7\u7a0b\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>7. \u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u5e38\u89c1\u5e94\u7528\u573a\u666f<\/strong><\/h3>\n\n\n\n<p>\u6b63\u5219\u8868\u8fbe\u5f0f\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\u6709\u5e7f\u6cdb\u7684\u5e94\u7528\uff0c\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u5e94\u7528\u573a\u666f\uff1a<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">7.1 \u5b57\u7b26\u4e32\u9a8c\u8bc1<\/h4>\n\n\n\n<p>\u6b63\u5219\u8868\u8fbe\u5f0f\u5e38\u5e38\u7528\u4e8e\u5bf9\u7528\u6237\u8f93\u5165\u8fdb\u884c\u9a8c\u8bc1\u3002\u4f8b\u5982\uff0c\u9a8c\u8bc1\u90ae\u7bb1\u3001\u624b\u673a\u53f7\u3001\u8eab\u4efd\u8bc1\u53f7\u7b49\u683c\u5f0f\u662f\u5426\u6b63\u786e\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.regex.*;\n\npublic class RegexExample {\n    public static void main(String&#91;] args) {\n        String email = \"user@example.com\";\n        String regex = \"^&#91;A-Za-z0-9+_.-]+@&#91;A-Za-z0-9.-]+$\";\n\n        Pattern pattern = Pattern.compile(regex);\n        Matcher matcher = pattern.matcher(email);\n\n        if (matcher.matches()) {\n            System.out.println(\"\u6709\u6548\u90ae\u7bb1\");\n        } else {\n            System.out.println(\"\u65e0\u6548\u90ae\u7bb1\");\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">7.2 \u6587\u672c\u641c\u7d22\u4e0e\u66ff\u6362<\/h4>\n\n\n\n<p>\u6b63\u5219\u8868\u8fbe\u5f0f\u53ef\u4ee5\u7528\u6765\u67e5\u627e\u548c\u66ff\u6362\u5b57\u7b26\u4e32\u4e2d\u7684\u67d0\u4e9b\u5185\u5bb9\u3002\u4f8b\u5982\uff0c\u5220\u9664\u6587\u672c\u4e2d\u7684 HTML \u6807\u7b7e\u6216\u66ff\u6362\u654f\u611f\u8bcd\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.regex.*;\n\npublic class RegexExample {\n    public static void main(String&#91;] args) {\n        String text = \"This is a &lt;b&gt;test&lt;\/b&gt; message.\";\n        String regex = \"&lt;&#91;^&gt;]*&gt;\";  \/\/ \u5339\u914d HTML \u6807\u7b7e\n\n        Pattern pattern = Pattern.compile(regex);\n        Matcher matcher = pattern.matcher(text);\n\n        \/\/ \u66ff\u6362 HTML \u6807\u7b7e\n        String result = matcher.replaceAll(\"\");\n        System.out.println(result);  \/\/ \u8f93\u51fa \"This is a test message.\"\n    }\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">7.3 \u4ece\u6587\u672c\u4e2d\u63d0\u53d6\u4fe1\u606f<\/h4>\n\n\n\n<p>\u6b63\u5219\u8868\u8fbe\u5f0f\u975e\u5e38\u9002\u5408\u7528\u6765\u4ece\u6587\u672c\u4e2d\u63d0\u53d6\u51fa\u7279\u5b9a\u7684\u4fe1\u606f\u3002\u4f8b\u5982\uff0c\u4ece\u65e5\u5fd7\u6587\u4ef6\u4e2d\u63d0\u53d6 IP \u5730\u5740\u3001\u65e5\u671f\u6216\u5176\u4ed6\u5173\u952e\u4fe1\u606f\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.regex.*;\n\npublic class RegexExample {\n    public static void main(String&#91;] args) {\n        String log = \"192.168.1.1 - - &#91;12\/Mar\/2025:15:20:01 +0000] \\\"GET \/index.html\\\"\";\n        String regex = \"(\\\\d+\\\\.\\\\d+\\\\.\\\\d+\\\\.\\\\d+)\";  \/\/ \u63d0\u53d6 IP \u5730\u5740\n\n        Pattern pattern = Pattern.compile(regex);\n        Matcher matcher = pattern.matcher(log);\n\n        if (matcher.find()) {\n            System.out.println(\"IP \u5730\u5740: \" + matcher.group(1));\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<p><strong>\u8f93\u51fa<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>IP \u5730\u5740: 192.168.1.1<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">7.4 \u5206\u6790\u4e0e\u5904\u7406\u6587\u4ef6\u683c\u5f0f<\/h4>\n\n\n\n<p>\u5728\u5904\u7406 CSV\u3001\u65e5\u5fd7\u6587\u4ef6\u3001XML\u3001JSON \u7b49\u6587\u4ef6\u683c\u5f0f\u65f6\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u4e00\u4e2a\u975e\u5e38\u5f3a\u5927\u7684\u5de5\u5177\u3002\u4f8b\u5982\uff0c\u5904\u7406 CSV \u6587\u4ef6\u4e2d\u7684\u5b57\u6bb5\u5206\u9694\u3001XML \u6807\u7b7e\u7684\u63d0\u53d6\u7b49\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.regex.*;\n\npublic class RegexExample {\n    public static void main(String&#91;] args) {\n        String csv = \"name,age,city\\nAlice,30,New York\\nBob,25,Boston\";\n        String regex = \"(\\\\w+),(\\\\d+),(\\\\w+)\";  \/\/ CSV \u6587\u4ef6\u4e2d\u7684\u6bcf\u4e00\u884c\n\n        Pattern pattern = Pattern.compile(regex);\n        Matcher matcher = pattern.matcher(csv);\n\n        while (matcher.find()) {\n            System.out.println(\"Name: \" + matcher.group(1) + \", Age: \" + matcher.group(2) + \", City: \" + matcher.group(3));\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<p><strong>\u8f93\u51fa<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Name: Alice, Age: 30, City: New York\nName: Bob, Age: 25, City: Boston<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>8. \u603b\u7ed3<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u57fa\u672c\u6982\u5ff5<\/strong>\uff1a\u901a\u8fc7\u4f7f\u7528\u5143\u5b57\u7b26\u548c\u7279\u6b8a\u8bed\u6cd5\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f\u5141\u8bb8\u4f60\u63cf\u8ff0\u548c\u5339\u914d\u590d\u6742\u7684\u5b57\u7b26\u4e32\u6a21\u5f0f\u3002<\/li>\n\n\n\n<li><strong>\u5e38\u89c1\u65b9\u6cd5<\/strong>\uff1a\u901a\u8fc7 <code>Pattern<\/code> \u548c <code>Matcher<\/code> \u7c7b\uff0c\u4f60\u53ef\u4ee5\u5728 Java \u4e2d\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u8fdb\u884c\u5b57\u7b26\u4e32\u5339\u914d\u3001\u66ff\u6362\u3001\u5206\u5272\u548c\u63d0\u53d6\u64cd\u4f5c\u3002<\/li>\n\n\n\n<li><strong>\u5e94\u7528\u573a\u666f<\/strong>\uff1a\u6b63\u5219\u8868\u8fbe\u5f0f\u5728\u9a8c\u8bc1\u8f93\u5165\u3001\u6587\u672c\u5904\u7406\u3001\u65e5\u5fd7\u5206\u6790\u3001\u6587\u4ef6\u89e3\u6790\u7b49\u65b9\u9762\u5177\u6709\u5e7f\u6cdb\u5e94\u7528\u3002<\/li>\n\n\n\n<li><strong>\u4f18\u5316<\/strong>\uff1a\u5408\u7406\u4f7f\u7528\u9884\u7f16\u8bd1\u3001\u975e\u8d2a\u5a6a\u6a21\u5f0f\u4ee5\u53ca\u907f\u514d\u8fc7\u4e8e\u590d\u6742\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u6709\u52a9\u4e8e\u63d0\u9ad8\u6027\u80fd\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u5982\u679c\u6709\u5176\u4ed6\u66f4\u590d\u6742\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u95ee\u9898\u6216\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\uff0c\u6b22\u8fce\u7ee7\u7eed\u63d0\u95ee\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Java \u6b63\u5219\u8868\u8fbe\u5f0f\uff08Regex\uff0cRegular Expression\uff09\u662f\u7528\u6765\u63cf\u8ff0\u5b57\u7b26\u4e32\u6a21\u5f0f\u7684\u5de5\u5177\uff0c\u5b83\u5141\u8bb8\u4f60\u68c0 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[68],"tags":[],"class_list":["post-2214","post","type-post","status-publish","format-standard","hentry","category-java"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2214","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=2214"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2214\/revisions"}],"predecessor-version":[{"id":2215,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2214\/revisions\/2215"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=2214"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=2214"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=2214"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}