{"id":508,"date":"2023-09-16T15:39:10","date_gmt":"2023-09-16T07:39:10","guid":{"rendered":""},"modified":"2023-09-17T00:42:44","modified_gmt":"2023-09-16T16:42:44","slug":"java-%e6%ad%a3%e5%88%99%e8%a1%a8%e8%be%be%e5%bc%8f%e5%8c%b9%e9%85%8d","status":"publish","type":"post","link":"https:\/\/mushiming.com\/508.html","title":{"rendered":"Java \u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d"},"content":{"rendered":"

1 \u6b63\u5219\u8868\u8fbe\u5f0f<\/h1>\n

1.1 \u4ec0\u4e48\u662f\u6b63\u5219\u8868\u8fbe\u5f0f<\/h2>\n

\u6b63\u5219\u8868\u8fbe\u5f0f: \u5b9a\u4e49\u4e00\u4e2a\u641c\u7d22\u6a21\u5f0f\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n

\u6b63\u5219\u8868\u8fbe\u5f0f\u53ef\u4ee5\u7528\u4e8e\u641c\u7d22\u3001\u7f16\u8f91\u548c\u64cd\u4f5c\u6587\u672c\u3002<\/p>\n

\u6b63\u5219\u5bf9\u6587\u672c\u7684\u5206\u6790\u6216\u4fee\u6539\u8fc7\u7a0b\u4e3a\uff1a\u9996\u5148\u6b63\u5219\u8868\u8fbe\u5f0f\u5e94\u7528\u7684\u662f\u6587\u672c\u5b57\u7b26\u4e32\uff08text\/string\uff09\uff0c\u5b83\u4f1a\u4ee5\u5b9a\u4e49\u7684\u6a21\u5f0f\u4ece\u5de6\u5230\u53f3\u5339\u914d\u6587\u672c\uff0c\u6bcf\u4e2a\u6e90\u5b57\u7b26\u53ea\u5339\u914d\u4e00\u6b21\u3002<\/p>\n

1.2 \u793a\u4f8b<\/h2>\n\n\n\n\n\n\n\n
\u6b63\u5219\u8868\u8fbe\u5f0f<\/th>\n\u5339\u914d<\/th>\n<\/tr>\n<\/thead>\n
this is text<\/code><\/td>\n\u7cbe\u786e\u5339\u914d\u5b57\u7b26\u4e32 \"this is text\"<\/td>\n<\/tr>\n
this\\s+is\\s+text<\/code><\/td>\n\u5339\u914d\u5355\u8bcd \"this\" \u540e\u8ddf\u4e00\u4e2a\u6216\u591a\u4e2a\u7a7a\u683c\u5b57\u7b26\uff0c\u540e\u8ddf\u8bcd \"is\" \u540e\u8ddf\u4e00\u4e2a\u6216\u591a\u4e2a\u7a7a\u683c\u5b57\u7b26\uff0c\u540e\u8ddf\u8bcd \"text\"<\/td>\n<\/tr>\n
^\\d+(\\.\\d+)?<\/code><\/td>\n^<\/code> \u5b9a\u4e49\u6a21\u5f0f\u5fc5\u987b\u5339\u914d\u5b57\u7b26\u4e32\u7684\u5f00\u59cb\uff0cd+<\/code> \u5339\u914d\u4e00\u4e2a\u6216\u591a\u4e2a\u6570\u5b57\uff0c?<\/code> \u8868\u660e\u5c0f\u62ec\u53f7\u5185\u7684\u8bed\u53e5\u662f\u53ef\u9009\u7684\uff0c\\.<\/code> \u5339\u914d \".\"\uff0c\u5c0f\u62ec\u53f7\u8868\u793a\u5206\u7ec4\u3002\u4f8b\u5982\u5339\u914d\uff1a\"5\"\u3001\"1.5\" \u548c \"2.21\"<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

2 \u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u7f16\u5199\u89c4\u5219<\/h1>\n

2.1 \u5e38\u89c1\u5339\u914d\u7b26\u53f7<\/h2>\n\n\n\n\n\n\n\n\n\n\n\n\n\n
\u6b63\u5219\u8868\u8fbe\u5f0f<\/th>\n\u63cf\u8ff0<\/th>\n<\/tr>\n<\/thead>\n
.<\/code><\/td>\n\u5339\u914d\u6240\u6709\u5355\u4e2a\u5b57\u7b26\uff0c\u9664\u4e86\u6362\u884c\u7b26\uff08Linux \u4e2d\u6362\u884c\u662f \\n<\/code>\uff0cWindows \u4e2d\u6362\u884c\u662f \\r\\n<\/code>\uff09<\/td>\n<\/tr>\n
^regex<\/code><\/td>\n\u6b63\u5219\u5fc5\u987b\u5339\u914d\u5b57\u7b26\u4e32\u5f00\u5934<\/td>\n<\/tr>\n
regex$<\/code><\/td>\n\u6b63\u5219\u5fc5\u987b\u5339\u914d\u5b57\u7b26\u4e32\u7ed3\u5c3e<\/td>\n<\/tr>\n
[abc]<\/code><\/td>\n\u590d\u9009\u96c6\u5b9a\u4e49\uff0c\u5339\u914d\u5b57\u6bcd a \u6216 b \u6216 c<\/td>\n<\/tr>\n
[abc][vz]<\/code><\/td>\n\u590d\u9009\u96c6\u5b9a\u4e49\uff0c\u5339\u914d\u5b57\u6bcd a \u6216 b \u6216 c\uff0c\u540e\u9762\u8ddf\u7740 v \u6216 z<\/td>\n<\/tr>\n
[^abc]<\/code><\/td>\n\u5f53\u63d2\u5165\u7b26 ^<\/code> \u5728\u4e2d\u62ec\u53f7\u4e2d\u4ee5\u7b2c\u4e00\u4e2a\u5b57\u7b26\u5f00\u59cb\u663e\u793a\uff0c\u5219\u8868\u793a\u5426\u5b9a\u6a21\u5f0f\u3002\u6b64\u6a21\u5f0f\u5339\u914d\u6240\u6709\u5b57\u7b26\uff0c\u9664\u4e86 a \u6216 b \u6216 c<\/td>\n<\/tr>\n
[a-d1-7]<\/code><\/td>\n\u8303\u56f4\u5339\u914d\uff0c\u5339\u914d\u5b57\u6bcd a \u5230 d \u548c\u6570\u5b57\u4ece 1 \u5230 7 \u4e4b\u95f4\uff0c\u4f46\u4e0d\u5339\u914d d1<\/td>\n<\/tr>\n
XZ<\/code><\/td>\n\u5339\u914d X \u540e\u76f4\u63a5\u8ddf\u7740 Z<\/td>\n<\/tr>\n
X|Z<\/td>\n\u5339\u914d X \u6216 Z<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

2.2 \u5143\u5b57\u7b26<\/h2>\n

\u5143\u5b57\u7b26\u662f\u4e00\u4e2a\u9884\u5b9a\u4e49\u7684\u5b57\u7b26\u3002<\/p>\n\n\n\n\n\n\n\n\n\n\n
\u6b63\u5219\u8868\u8fbe\u5f0f<\/th>\n\u63cf\u8ff0<\/th>\n<\/tr>\n<\/thead>\n
\\d<\/code><\/td>\n\u5339\u914d\u4e00\u4e2a\u6570\u5b57\uff0c\u662f [0-9]<\/code> \u7684\u7b80\u5199<\/td>\n<\/tr>\n
\\D<\/code><\/td>\n\u5339\u914d\u4e00\u4e2a\u975e\u6570\u5b57\uff0c\u662f [^0-9]<\/code> \u7684\u7b80\u5199<\/td>\n<\/tr>\n
\\s<\/code><\/td>\n\u5339\u914d\u4e00\u4e2a\u7a7a\u683c\uff0c\u662f [ \\t\\n\\x0b\\r\\f]<\/code> \u7684\u7b80\u5199<\/td>\n<\/tr>\n
\\S<\/code><\/td>\n\u5339\u914d\u4e00\u4e2a\u975e\u7a7a\u683c<\/td>\n<\/tr>\n
\\w<\/code><\/td>\n\u5339\u914d\u4e00\u4e2a\u5355\u8bcd\u5b57\u7b26\uff08\u5927\u5c0f\u5199\u5b57\u6bcd\u3001\u6570\u5b57\u3001\u4e0b\u5212\u7ebf\uff09\uff0c\u662f [a-zA-Z_0-9]<\/code> \u7684\u7b80\u5199<\/td>\n<\/tr>\n
\\W<\/code><\/td>\n\u5339\u914d\u4e00\u4e2a\u975e\u5355\u8bcd\u5b57\u7b26\uff08\u9664\u4e86\u5927\u5c0f\u5199\u5b57\u6bcd\u3001\u6570\u5b57\u3001\u4e0b\u5212\u7ebf\u4e4b\u5916\u7684\u5b57\u7b26\uff09\uff0c\u7b49\u540c\u4e8e [^\\w]<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

2.3 \u9650\u5b9a\u7b26<\/h2>\n

\u9650\u5b9a\u7b26\u5b9a\u4e49\u4e86\u4e00\u4e2a\u5143\u7d20\u53ef\u4ee5\u53d1\u751f\u7684\u9891\u7387\u3002<\/p>\n\n\n\n\n\n\n\n\n\n\n
\u6b63\u5219\u8868\u8fbe\u5f0f<\/th>\n\u63cf\u8ff0<\/th>\n\u4e3e\u4f8b<\/th>\n<\/tr>\n<\/thead>\n
*<\/code><\/td>\n\u5339\u914d >=0 \u4e2a\uff0c\u662f {0,}<\/code> \u7684\u7b80\u5199<\/td>\nX*<\/code> \u8868\u793a\u5339\u914d\u96f6\u4e2a\u6216\u591a\u4e2a\u5b57\u6bcd X\uff0c.*<\/code> \u8868\u793a\u5339\u914d\u4efb\u4f55\u5b57\u7b26\u4e32<\/td>\n<\/tr>\n
+<\/code><\/td>\n\u5339\u914d >=1 \u4e2a\uff0c\u662f {1,}<\/code> \u7684\u7b80\u5199<\/td>\nX+<\/code> \u8868\u793a\u5339\u914d\u4e00\u4e2a\u6216\u591a\u4e2a\u5b57\u6bcd X<\/td>\n<\/tr>\n
?<\/code><\/td>\n\u5339\u914d 1 \u4e2a\u6216 0 \u4e2a\uff0c\u662f {0,1}<\/code> \u7684\u7b80\u5199<\/td>\nX?<\/code> \u8868\u793a\u5339\u914d 0 \u4e2a\u6216 1 \u4e2a\u5b57\u6bcd X<\/td>\n<\/tr>\n
{X}<\/code><\/td>\n\u53ea\u5339\u914d X \u4e2a\u5b57\u7b26<\/td>\n\\d{3}<\/code> \u8868\u793a\u5339\u914d 3 \u4e2a\u6570\u5b57\uff0c.{10}<\/code> \u8868\u793a\u5339\u914d\u4efb\u4f55\u957f\u5ea6\u662f 10 \u7684\u5b57\u7b26\u4e32<\/td>\n<\/tr>\n
{X,Y}<\/code><\/td>\n\u5339\u914d >=X \u4e14 <=Y \u4e2a<\/td>\n\\d{1,4}<\/code> \u8868\u793a\u5339\u914d\u81f3\u5c11 1 \u4e2a\u6700\u591a 4 \u4e2a\u6570\u5b57<\/td>\n<\/tr>\n
*?<\/code><\/td>\n\u5982\u679c ?<\/code> \u662f\u9650\u5b9a\u7b26 *<\/code> \u6216 +<\/code> \u6216 ?<\/code> \u6216 {}<\/code> \u540e\u9762\u7684\u7b2c\u4e00\u4e2a\u5b57\u7b26\uff0c\u90a3\u4e48\u8868\u793a\u975e\u8d2a\u5a6a\u6a21\u5f0f<\/strong>\uff08\u5c3d\u53ef\u80fd\u5c11\u7684\u5339\u914d\u5b57\u7b26\uff09\uff0c\u800c\u4e0d\u662f\u9ed8\u8ba4\u7684\u8d2a\u5a6a\u6a21\u5f0f<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

2.4 \u5206\u7ec4\u548c\u53cd\u5411\u5f15\u7528<\/h2>\n

\u5c0f\u62ec\u53f7 ()<\/code> \u53ef\u4ee5\u8fbe\u5230\u5bf9\u6b63\u5219\u8868\u8fbe\u5f0f\u8fdb\u884c\u5206\u7ec4\u7684\u6548\u679c\u3002<\/p>\n

\u6a21\u5f0f\u5206\u7ec4\u540e\u4f1a\u5728\u6b63\u5219\u8868\u8fbe\u5f0f\u4e2d\u521b\u5efa\u53cd\u5411\u5f15\u7528<\/strong>\u3002\u53cd\u5411\u5f15\u7528\u4f1a\u4fdd\u5b58\u5339\u914d\u6a21\u5f0f\u5206\u7ec4\u7684\u5b57\u7b26\u4e32\u7247\u65ad\uff0c\u8fd9\u4f7f\u5f97\u6211\u4eec\u53ef\u4ee5\u83b7\u53d6\u5e76\u4f7f\u7528\u8fd9\u4e2a\u5b57\u7b26\u4e32\u7247\u65ad\u3002<\/p>\n

\u5728\u4ee5\u6b63\u5219\u8868\u8fbe\u5f0f\u66ff\u6362\u5b57\u7b26\u4e32\u7684\u8bed\u6cd5\u4e2d\uff0c\u662f\u901a\u8fc7 $<\/code> \u6765\u5f15\u7528\u5206\u7ec4\u7684\u53cd\u5411\u5f15\u7528\uff0c$0<\/code> \u662f\u5339\u914d\u5b8c\u6574\u6a21\u5f0f\u7684\u5b57\u7b26\u4e32\uff08\u6ce8\u610f\u5728 JavaScript \u4e2d\u662f\u7528 $&<\/code> \u8868\u793a\uff09\uff1b$1<\/code> \u662f\u7b2c\u4e00\u4e2a\u5206\u7ec4\u7684\u53cd\u5411\u5f15\u7528\uff1b$2<\/code> \u662f\u7b2c\u4e8c\u4e2a\u5206\u7ec4\u7684\u53cd\u5411\u5f15\u7528\uff0c\u4ee5\u6b64\u7c7b\u63a8\u3002<\/p>\n

\u793a\u4f8b\uff1a<\/p>\n

\npublic class RegexTest {\n\n    public static void main(String[] args) {\n        \/\/ \u53bb\u9664\u5355\u8bcd\u4e0e , \u548c . \u4e4b\u95f4\u7684\u7a7a\u683c\n        String Str = \"Hello , World .\";\n        String pattern = \"(\\\\w)(\\\\s+)([.,])\";\n        \/\/ $0 \u5339\u914d `(\\w)(\\s+)([.,])` \u7ed3\u679c\u4e3a `o\u7a7a\u683c,` \u548c `d\u7a7a\u683c.`\n        \/\/ $1 \u5339\u914d `(\\w)` \u7ed3\u679c\u4e3a `o` \u548c `d`\n        \/\/ $2 \u5339\u914d `(\\s+)` \u7ed3\u679c\u4e3a `\u7a7a\u683c` \u548c `\u7a7a\u683c`\n        \/\/ $3 \u5339\u914d `([.,])` \u7ed3\u679c\u4e3a `,` \u548c `.`\n        System.out.println(Str.replaceAll(pattern, \"$1$3\")); \/\/ Hello, World.\n    }\n}<\/code><\/pre>\n

\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86 [.]<\/code> \u6765\u5339\u914d\u666e\u901a\u5b57\u7b26 .<\/code> \u800c\u4e0d\u9700\u8981\u4f7f\u7528 [\\\\.]<\/code>\u3002\u56e0\u4e3a\u6b63\u5219\u5bf9\u4e8e []<\/code> \u4e2d\u7684 .<\/code>\uff0c\u4f1a\u81ea\u52a8\u5904\u7406\u4e3a [\\.]<\/code>\uff0c\u5373\u666e\u901a\u5b57\u7b26 .<\/code> \u8fdb\u884c\u5339\u914d\u3002<\/p>\n

2.4.1 \u4ec5\u5206\u7ec4\u4f46\u65e0\u53cd\u5411\u5f15\u7528<\/h3>\n

\u5f53\u6211\u4eec\u5728\u5c0f\u62ec\u53f7 ()<\/code> \u5185\u7684\u6a21\u5f0f\u5f00\u5934\u52a0\u5165 ?:<\/code>\uff0c\u90a3\u4e48\u8868\u793a\u8fd9\u4e2a\u6a21\u5f0f\u4ec5\u5206\u7ec4\uff0c\u4f46\u4e0d\u521b\u5efa\u53cd\u5411\u5f15\u7528\u3002<\/p>\n

\u793a\u4f8b\uff1a<\/p>\n

import java.util.regex.Matcher;\nimport java.util.regex.Pattern;\n\npublic class RegexTest {\n\n    public static void main(String[] args) {\n        String str = \"img.jpg\";\n        \/\/ \u5206\u7ec4\u4e14\u521b\u5efa\u53cd\u5411\u5f15\u7528\n        Pattern pattern = Pattern.compile(\"(jpg|png)\");\n        Matcher matcher = pattern.matcher(str);\n        while (matcher.find()) {\n            System.out.println(matcher.group());\n            System.out.println(matcher.group(1));\n        }\n    }\n}<\/code><\/pre>\n

\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n

\n

jpg jpg<\/p>\n<\/blockquote>\n

\u82e5\u6e90\u7801\u6539\u4e3a\uff1a<\/p>\n

import java.util.regex.Matcher;\nimport java.util.regex.Pattern;\n\npublic class RegexTest {\n\n    public static void main(String[] args) {\n        String str = \"img.jpg\";\n        \/\/ \u5206\u7ec4\u4f46\u4e0d\u521b\u5efa\u53cd\u5411\u5f15\u7528\n        Pattern pattern = Pattern.compile(\"(?:jpg|png)\");\n        Matcher matcher = pattern.matcher(str);\n        while (matcher.find()) {\n            System.out.println(matcher.group());\n            System.out.println(matcher.group(1));\n        }\n    }\n}<\/code><\/pre>\n

\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n

\n
jpg\nException in thread \"main\" java.lang.IndexOutOfBoundsException: No group 1\n    at java.util.regex.Matcher.group(Matcher.java:538)\n    at com.wuxianjiezh.regex.RegexTest.main(RegexTest.java:15)<\/pre>\n<\/blockquote>\n

2.4.2 \u5206\u7ec4\u7684\u53cd\u5411\u5f15\u7528\u526f\u672c<\/h3>\n

Java \u4e2d\u53ef\u4ee5\u5728\u5c0f\u62ec\u53f7\u4e2d\u4f7f\u7528 ?<name><\/code> \u5c06\u5c0f\u62ec\u53f7\u4e2d\u5339\u914d\u7684\u5185\u5bb9\u4fdd\u5b58\u4e3a\u4e00\u4e2a\u540d\u5b57\u4e3a name \u7684\u526f\u672c\u3002<\/p>\n

\u793a\u4f8b\uff1a<\/p>\n

import java.util.regex.Matcher;\nimport java.util.regex.Pattern;\n\npublic class RegexTest {\n\n    public static void main(String[] args) {\n        String str = \"@wkf \u4f60\u597d\u554a\";\n        Pattern pattern = Pattern.compile(\"@(?<first>\\\\w+\\\\s)\"); \/\/ \u4fdd\u5b58\u4e00\u4e2a\u526f\u672c\n        Matcher matcher = pattern.matcher(str);\n        while (matcher.find()) {\n            System.out.println(matcher.group());\n            System.out.println(matcher.group(1));\n            System.out.println(matcher.group(\"first\"));\n        }\n    }\n}<\/code><\/pre>\n

\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n

\n
@wkf\nwkf \nwkf <\/pre>\n<\/blockquote>\n

2.5 \u5426\u5b9a\u5148\u884c\u65ad\u8a00\uff08Negative lookahead\uff09<\/h2>\n

\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u5426\u5b9a\u5148\u884c\u65ad\u8a00\u6a21\u5f0f\u7684\u5339\u914d\uff0c\u5373\u67d0\u4e2a\u5b57\u7b26\u4e32\u540e\u9762\u4e0d\u5305\u542b\u53e6\u4e00\u4e2a\u5b57\u7b26\u4e32\u7684\u5339\u914d\u6a21\u5f0f\u3002<\/p>\n

\u5426\u5b9a\u5148\u884c\u65ad\u8a00\u6a21\u5f0f\u901a\u8fc7 (?!pattern)<\/code> \u5b9a\u4e49\u3002\u6bd4\u5982\uff0c\u6211\u4eec\u5339\u914d\u540e\u9762\u4e0d\u662f\u8ddf\u7740 \"b\" \u7684 \"a\"\uff1a<\/p>\n

\n
a(?!b)<\/pre>\n<\/blockquote>\n

2.6 \u6307\u5b9a\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u6a21\u5f0f<\/h2>\n

\u53ef\u4ee5\u5728\u6b63\u5219\u7684\u5f00\u5934<\/strong>\u6307\u5b9a\u6a21\u5f0f\u4fee\u9970\u7b26\u3002<\/p>\n

    \n
  • (?i)<\/code> \u4f7f\u6b63\u5219\u5ffd\u7565\u5927\u5c0f\u5199\u3002<\/li>\n
  • (?s)<\/code> \u8868\u793a\u5355\u884c\u6a21\u5f0f<\/em>\uff08\"single line mode\"\uff09\u4f7f\u6b63\u5219\u7684 .<\/code> \u5339\u914d\u6240\u6709\u5b57\u7b26\uff0c\u5305\u62ec\u6362\u884c\u7b26\u3002<\/li>\n
  • (?m)<\/code> \u8868\u793a\u591a\u884c\u6a21\u5f0f<\/em>\uff08\"multi-line mode\"\uff09\uff0c\u4f7f\u6b63\u5219\u7684 ^<\/code> \u548c $<\/code> \u5339\u914d\u5b57\u7b26\u4e32\u4e2d\u6bcf\u884c\u7684\u5f00\u59cb\u548c\u7ed3\u675f\u3002<\/li>\n<\/ul>\n

    2.7 Java \u4e2d\u7684\u53cd\u659c\u6760<\/h2>\n

    \u53cd\u659c\u6760 \\<\/code> \u5728 Java \u4e2d\u8868\u793a\u8f6c\u4e49\u5b57\u7b26\uff0c\u8fd9\u610f\u5473\u7740 \\<\/code> \u5728 Java \u62e5\u6709\u9884\u5b9a\u4e49\u7684\u542b\u4e49\u3002<\/p>\n

    \u8fd9\u91cc\u4f8b\u4e3e\u4e24\u4e2a\u7279\u522b\u91cd\u8981\u7684\u7528\u6cd5\uff1a<\/p>\n

      \n
    • \u5728\u5339\u914d .<\/code> \u6216 {
      \n <\/code> \u6216 [<\/code> \u6216 (<\/code> \u6216 ?<\/code> \u6216 $<\/code> \u6216 ^<\/code> \u6216 *<\/code> \u8fd9\u4e9b\u7279\u6b8a\u5b57\u7b26\u65f6\uff0c\u9700\u8981\u5728\u524d\u9762\u52a0\u4e0a \\\\<\/code>\uff0c\u6bd4\u5982\u5339\u914d .<\/code> \u65f6\uff0cJava \u4e2d\u8981\u5199\u4e3a \\\\.<\/code>\uff0c\u4f46\u5bf9\u4e8e\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u8bf4\u5c31\u662f \\.<\/code>\u3002<\/li>\n
    • \u5728\u5339\u914d \\<\/code> \u65f6\uff0cJava \u4e2d\u8981\u5199\u4e3a \\\\\\\\<\/code>\uff0c\u4f46\u5bf9\u4e8e\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u8bf4\u5c31\u662f \\\\<\/code>\u3002<\/li>\n<\/ul>\n

      \u6ce8\u610f<\/strong>\uff1aJava \u4e2d\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u5b57\u7b26\u4e32\u6709\u4e24\u5c42\u542b\u4e49\uff0c\u9996\u5148 Java \u5b57\u7b26\u4e32\u8f6c\u4e49\u51fa\u7b26\u5408\u6b63\u5219\u8868\u8fbe\u5f0f\u8bed\u6cd5\u7684\u5b57\u7b26\u4e32\uff0c\u7136\u540e\u518d\u7531\u8f6c\u4e49\u540e\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u8fdb\u884c\u6a21\u5f0f\u5339\u914d\u3002<\/p>\n

      2.8 \u6613\u9519\u70b9\u793a\u4f8b<\/h2>\n
        \n
      • [jpg|png]<\/code> \u4ee3\u8868\u5339\u914d j<\/code> \u6216 p<\/code> \u6216 g<\/code> \u6216 p<\/code> \u6216 n<\/code> \u6216 g<\/code> \u4e2d\u7684\u4efb\u610f\u4e00\u4e2a\u5b57\u7b26\u3002<\/li>\n
      • (jpg|png)<\/code> \u4ee3\u8868\u5339\u914d jpg<\/code> \u6216 png<\/code>\u3002<\/li>\n<\/ul>\n

        3 \u5728\u5b57\u7b26\u4e32\u4e2d\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f<\/h1>\n

        3.1 \u5185\u7f6e\u7684\u5b57\u7b26\u4e32\u6b63\u5219\u5904\u7406\u65b9\u6cd5<\/h2>\n

        \u5728 Java \u4e2d\u6709\u56db\u4e2a\u5185\u7f6e\u7684\u8fd0\u884c\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u65b9\u6cd5\uff0c\u5206\u522b\u662f matches()<\/code>\u3001split())<\/code>\u3001replaceFirst()<\/code>\u3001replaceAll()<\/code>\u3002\u6ce8\u610f replace()<\/code> \u65b9\u6cd5\u4e0d\u652f\u6301\u6b63\u5219\u8868\u8fbe\u5f0f\u3002<\/p>\n\n\n\n\n\n\n\n\n
        \u65b9\u6cd5<\/th>\n\u63cf\u8ff0<\/th>\n<\/tr>\n<\/thead>\n
        s.matches(\"regex\")<\/code><\/td>\n\u5f53\u4ec5\u4e14\u5f53\u6b63\u5219\u5339\u914d\u6574\u4e2a\u5b57\u7b26\u4e32\u65f6\u8fd4\u56de true<\/code><\/td>\n<\/tr>\n
        s.split(\"regex\")<\/code><\/td>\n\u6309\u5339\u914d\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u5207\u7247\u5b57\u7b26\u4e32<\/td>\n<\/tr>\n
        s.replaceFirst(\"regex\", \"replacement\")<\/code><\/td>\n\u66ff\u6362\u9996\u6b21\u5339\u914d\u7684\u5b57\u7b26\u4e32\u7247\u6bb5<\/td>\n<\/tr>\n
        s.replaceAll(\"regex\", \"replacement\")<\/code><\/td>\n\u66ff\u6362\u6240\u6709\u5339\u914d\u7684\u5b57\u7b26<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

        3.2 \u4ee3\u7801\u793a\u4f8b <\/h2>\n<\/p>\n
        public class RegexTest {\n\n    public static void main(String[] args) {\n        System.out.println(\"wxj\".matches(\"wxj\"));\n        System.out.println(\"----------\");\n\n        String[] array = \"w x j\".split(\"\\\\s\");\n        for (String item : array) {\n            System.out.println(item);\n        }\n        System.out.println(\"----------\");\n\n        System.out.println(\"w x j\".replaceFirst(\"\\\\s\", \"-\"));\n        System.out.println(\"----------\");\n\n        System.out.println(\"w x j\".replaceAll(\"\\\\s\", \"-\"));\n    }\n}<\/code><\/pre>\n

         \u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n

        \n
        true\n----------\nw\nx\nj\n----------\nw-x j\n----------\nw-x-j<\/pre>\n<\/blockquote>\n

        4 \u6a21\u5f0f\u548c\u5339\u914d<\/h1>\n

        Java \u4e2d\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u9700\u8981\u7528\u5230\u4e24\u4e2a\u7c7b\uff0c\u5206\u522b\u4e3a java.util.regex.Pattern<\/code> \u548c java.util.regex.Matcher<\/code>\u3002<\/p>\n

        \u7b2c\u4e00\u6b65\uff0c\u901a\u8fc7\u6b63\u5219\u8868\u8fbe\u5f0f\u521b\u5efa\u6a21\u5f0f\u5bf9\u8c61 Pattern<\/code>\u3002<\/p>\n

        \u7b2c\u4e8c\u6b65\uff0c\u901a\u8fc7\u6a21\u5f0f\u5bf9\u8c61 Pattern<\/code>\uff0c\u6839\u636e\u6307\u5b9a\u5b57\u7b26\u4e32\u521b\u5efa\u5339\u914d\u5bf9\u8c61 Matcher<\/code>\u3002<\/p>\n

        \u7b2c\u4e09\u6b65\uff0c\u901a\u8fc7\u5339\u914d\u5bf9\u8c61 Matcher<\/code>\uff0c\u6839\u636e\u6b63\u5219\u8868\u8fbe\u5f0f\u64cd\u4f5c\u5b57\u7b26\u4e32\u3002<\/p>\n

        \u6765\u4e2a\u4f8b\u5b50\uff0c\u52a0\u6df1\u7406\u89e3\uff1a<\/p>\n

        import java.util.regex.Matcher;\nimport java.util.regex.Pattern;\n\npublic class RegexTest {\n\n    public static void main(String[] args) {\n        String text = \"Hello Regex!\";\n\n        Pattern pattern = Pattern.compile(\"\\\\w+\");\n        \/\/ Java \u4e2d\u5ffd\u7565\u5927\u5c0f\u5199\uff0c\u6709\u4e24\u79cd\u5199\u6cd5\uff1a\n        \/\/ Pattern pattern = Pattern.compile(\"\\\\w+\", Pattern.CASE_INSENSITIVE);\n        \/\/ Pattern pattern = Pattern.compile(\"(?i)\\\\w+\"); \/\/ \u63a8\u8350\u5199\u6cd5\n        Matcher matcher = pattern.matcher(text);\n        \/\/ \u904d\u4f8b\u6240\u6709\u5339\u914d\u7684\u5e8f\u5217\n        while (matcher.find()) {\n            System.out.print(\"Start index: \" + matcher.start());\n            System.out.print(\" End index: \" + matcher.end() + \" \");\n            System.out.println(matcher.group());\n        }\n        \/\/ \u521b\u5efa\u7b2c\u4e24\u4e2a\u6a21\u5f0f\uff0c\u5c06\u7a7a\u683c\u66ff\u6362\u4e3a tab\n        Pattern replace = Pattern.compile(\"\\\\s+\");\n        Matcher matcher2 = replace.matcher(text);\n        System.out.println(matcher2.replaceAll(\"\\t\"));\n    }\n}<\/code><\/pre>\n

        \u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n

        \n
        Start index: 0 End index: 5 Hello\nStart index: 6 End index: 11 Regex\nHello    Regex!<\/pre>\n<\/blockquote>\n

        5 \u82e5\u5e72\u4e2a\u5e38\u7528\u4f8b\u5b50<\/h1>\n

        5.1 \u4e2d\u6587\u7684\u5339\u914d<\/h2>\n

        [\\u4e00-\\u9fa5]+<\/code><\/strong> \u4ee3\u8868\u5339\u914d\u4e2d\u6587\u5b57\u3002<\/p>\n

        import java.util.regex.Matcher;\nimport java.util.regex.Pattern;\n\npublic class RegexTest {\n\n    public static void main(String[] args) {\n        String str = \"\u8fd9\u662f\u4e2d\u6587\";\n        Pattern pattern = Pattern.compile(\"[\\\\u4e00-\\\\u9fa5]+\");\n        Matcher matcher = pattern.matcher(str);\n        while (matcher.find()) {\n            System.out.println(matcher.group());\n        }\n    }\n}<\/code><\/pre>\n

        \u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n

        \n

        \u8fd9\u662f\u4e2d\u6587<\/p>\n<\/blockquote>\n

        5.2 \u6570\u5b57\u8303\u56f4\u7684\u5339\u914d<\/h2>\n

        \u6bd4\u5982\uff0c\u5339\u914d 1990 \u5230 2017\u3002<\/p>\n

        \u6ce8\u610f\uff1a<\/strong>\u8fd9\u91cc\u6709\u4e2a\u65b0\u624b\u6613\u8303\u7684\u9519\u8bef\uff0c\u5c31\u662f\u6b63\u5219 [1990-2017]<\/code>\uff0c\u5b9e\u9645\u8fd9\u4e2a\u6b63\u5219\u53ea\u5339\u914d 0<\/code> \u6216 1<\/code> \u6216 2<\/code> \u6216 7<\/code> \u6216 9<\/code> \u4e2d\u7684\u4efb\u4e00\u4e2a\u5b57\u7b26\u3002<\/p>\n

        \u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u6570\u5b57\u8303\u56f4\u65f6\uff0c\u9996\u5148\u8981\u786e\u5b9a\u6700\u5927\u503c\u4e0e\u6700\u5c0f\u503c\uff0c\u6700\u540e\u5199\u4e2d\u95f4\u503c\u3002<\/p>\n

        \u6b63\u786e\u7684\u5339\u914d\u65b9\u5f0f\uff1a<\/p>\n

        import java.util.regex.Matcher;\nimport java.util.regex.Pattern;\n\npublic class RegexTest {\n\n    public static void main(String[] args) {\n        String str = \"1990\\n2010\\n2017\";\n        \/\/ \u8fd9\u91cc\u5e94\u7528\u4e86 (?m) \u7684\u591a\u884c\u5339\u914d\u6a21\u5f0f\uff0c\u53ea\u4e3a\u65b9\u4fbf\u6211\u4eec\u6d4b\u8bd5\u8f93\u51fa\n        \/\/ \"^1990$|^199[1-9]$|^20[0-1][0-6]$|^2017$\" \u4e3a\u5224\u65ad 1990-2017 \u6b63\u786e\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\n        Pattern pattern = Pattern.compile(\"(?m)^1990$|^199[1-9]$|^20[0-1][0-6]$|^2017$\");\n        Matcher matcher = pattern.matcher(str);\n        while (matcher.find()) {\n            System.out.println(matcher.group());\n        }\n    }\n}<\/code><\/pre>\n

        \u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n

        \n
        1990\n2010\n2017<\/pre>\n<\/blockquote>\n

        5.3 img \u6807\u7b7e\u7684\u5339\u914d<\/h2>\n

        \u6bd4\u5982\uff0c\u83b7\u53d6\u56fe\u7247\u6587\u4ef6\u5185\u5bb9\uff0c\u8fd9\u91cc\u6211\u4eec\u8003\u8651\u4e86\u4e00\u4e9b\u4e0d\u89c4\u8303\u7684 img \u6807\u7b7e\u5199\u6cd5\uff1a<\/p>\n

        import java.util.regex.Matcher;\nimport java.util.regex.Pattern;\n\npublic class RegexTest {\n\n    public static void main(String[] args) {\n        String str = \"<img  src='aaa.jpg' \/><img src=bbb.png\/><img src=\\\"ccc.png\\\"\/>\" +\n                \"<img src='ddd.exe'\/><img src='eee.jpn'\/>\";\n        \/\/ \u8fd9\u91cc\u6211\u4eec\u8003\u8651\u4e86\u4e00\u4e9b\u4e0d\u89c4\u8303\u7684 img \u6807\u7b7e\u5199\u6cd5\uff0c\u6bd4\u5982\uff1a\u7a7a\u683c\u3001\u5f15\u53f7\n        Pattern pattern = Pattern.compile(\"<img\\\\s+src=(?:['\\\"])?(?<src>\\\\w+.(jpg|png))(?:['\\\"])?\\\\s*\/>\");\n        Matcher matcher = pattern.matcher(str);\n        while (matcher.find()) {\n            System.out.println(matcher.group(\"src\"));\n        }\n    }\n}<\/code><\/pre>\n

        \u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n

        \n
        aaa.jpg\nbbb.png\nccc.png<\/pre>\n<\/blockquote>\n

        5.4 \u8d2a\u5a6a\u4e0e\u975e\u8d2a\u5a6a\u6a21\u5f0f\u7684\u5339\u914d<\/h2>\n

        \u6bd4\u5982\uff0c\u83b7\u53d6 div \u6807\u7b7e\u4e2d\u7684\u6587\u672c\u5185\u5bb9\uff1a<\/p>\n

        import java.util.regex.Matcher;\nimport java.util.regex.Pattern;\n\npublic class RegexTest {\n\n    public static void main(String[] args) {\n        String str = \"<div>\u6587\u7ae0\u6807\u9898<\/div><div>\u53d1\u5e03\u65f6\u95f4<\/div>\";\n        \/\/ \u8d2a\u5a6a\u6a21\u5f0f\n        Pattern pattern = Pattern.compile(\"<div>(?<title>.+)<\/div>\");\n        Matcher matcher = pattern.matcher(str);\n        while (matcher.find()) {\n            System.out.println(matcher.group(\"title\"));\n        }\n\n        System.out.println(\"--------------\");\n\n        \/\/ \u975e\u8d2a\u5a6a\u6a21\u5f0f\n        pattern = Pattern.compile(\"<div>(?<title>.+?)<\/div>\");\n        matcher = pattern.matcher(str);\n        while (matcher.find()) {\n            System.out.println(matcher.group(\"title\"));\n        }\n    }\n}<\/code><\/pre>\n

        \u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n

        \n
        \u6587\u7ae0\u6807\u9898<\/div><div>\u53d1\u5e03\u65f6\u95f4\n--------------\n\u6587\u7ae0\u6807\u9898\n\u53d1\u5e03\u65f6\u95f4<\/pre>\n<\/blockquote>\n

        6 \u63a8\u8350\u4e24\u4e2a\u5728\u7ebf\u6b63\u5219\u5de5\u5177<\/h1>\n
          \n
        • JavaScript\u3001Python \u7b49\u7684\u5728\u7ebf\u8868\u8fbe\u5f0f\u5de5\u5177\uff1ahttps:\/\/regex101.com\/<\/li>\n
        • Java \u5728\u7ebf\u8868\u8fbe\u5f0f\u5de5\u5177\uff1ahttp:\/\/www.regexplanet.com\/advanced\/java\/index.html<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"Java \u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914dJava\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d_java\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[82],"tags":[],"_links":{"self":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/508"}],"collection":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/comments?post=508"}],"version-history":[{"count":0,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/508\/revisions"}],"wp:attachment":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/media?parent=508"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/categories?post=508"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/tags?post=508"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}