{"id":199445,"date":"2020-09-03T10:08:44","date_gmt":"2020-09-03T02:08:44","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=199445"},"modified":"2020-08-28T10:09:32","modified_gmt":"2020-08-28T02:09:32","slug":"java-regular-expression","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/java-regular-expression.html","title":{"rendered":"Java \u6b63\u5219\u8868\u8fbe\u5f0f\u6355\u83b7\u7ec4\u5206\u7c7b"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u4ec0\u4e48\u662f\u6355\u83b7\u7ec4\uff1f\u7b80\u5355\u70b9\u8bf4\uff0c\u6355\u83b7\u7ec4\u5c31\u662f\u628a\uff08Expression\uff09\u4e2d\u5339\u914d\u5230\u7684\u5185\u5bb9\u4fdd\u5b58\u5230\u4e00\u4e2a\u6309\u201c\uff08\u201d\u51fa\u73b0\u7684\u987a\u5e8f\u7f16\u53f7\u7684\u7ec4\u91cc\uff0c\u4ee5\u4f9b\u540e\u7eed\u5f15\u7528\uff0c\u5f15\u7528\u7684\u65b9\u5f0f\u6709\u53cd\u5411\u5f15\u7528\uff0c\u6216\u662fRegExp.Snumber\u7b49\u65b9\u5f0f\uff0c\u4e0d\u540c\u7684\u8bed\u8a00\uff0c\u652f\u6301\u7684\u5f15\u7528\u65b9\u5f0f\u4e0d\u540c\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\"\"
\nJava \u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u6355\u83b7\u7ec4\u5206\u4e3a\uff1a
\n\u666e\u901a\u6355\u83b7\u7ec4(Expression)
\n\u547d\u540d\u6355\u83b7\u7ec4(?Expression)<\/p>\n

\u666e\u901a\u6355\u83b7\u7ec4<\/strong><\/div>\n

\u4ece\u6b63\u5219\u8868\u8fbe\u5f0f\u5de6\u4fa7\u5f00\u59cb\uff0c\u6bcf\u51fa\u73b0\u4e00\u4e2a\u5de6\u62ec\u53f7\"(\"\u8bb0\u505a\u4e00\u4e2a\u5206\u7ec4\uff0c\u5206\u7ec4\u7f16\u53f7\u4ece 1 \u5f00\u59cb\u30020 \u4ee3\u8868\u6574\u4e2a\u8868\u8fbe\u5f0f\u3002<\/p>\n

\u5bf9\u4e8e\u65f6\u95f4\u5b57\u7b26\u4e32\uff1a2017-04-25\uff0c\u8868\u8fbe\u5f0f\u5982\u4e0b<\/p>\n

(\\\\d{4})-((\\\\d{2})-(\\\\d{2}))<\/pre>\n

\u6709 4 \u4e2a\u5de6\u62ec\u53f7\uff0c\u6240\u4ee5\u6709 4 \u4e2a\u5206\u7ec4:<\/p>\n\n\n\n\n\n\n\n\n\n
\u7f16\u53f7<\/th>\n\u6355\u83b7\u7ec4<\/th>\n\u5339\u914d<\/th>\n<\/tr>\n<\/thead>\n
0<\/td>\n(\\d{4})-((\\d{2})-(\\d{2}))<\/td>\n2017-04-25<\/td>\n<\/tr>\n
1<\/td>\n(\\d{4})<\/td>\n2017<\/td>\n<\/tr>\n
2<\/td>\n((\\d{2})-(\\d{2}))<\/td>\n04-25<\/td>\n<\/tr>\n
3<\/td>\n(\\d{2})<\/td>\n04<\/td>\n<\/tr>\n
4<\/td>\n(\\d{2})<\/td>\n25<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n
public static final String DATE_STRING = \"2017-04-25\";\r\npublic static final String P_COMM = \"(\\\\d{4})-((\\\\d{2})-(\\\\d{2}))\";\r\n \r\nPattern pattern = Pattern.compile(P_COMM);\r\nMatcher matcher = pattern.matcher(DATE_STRING);\r\nmatcher.find();\/\/\u5fc5\u987b\u8981\u6709\u8fd9\u53e5\r\nSystem.out.printf(\"\\nmatcher.group(0) value:%s\", matcher.group(0));\r\nSystem.out.printf(\"\\nmatcher.group(1) value:%s\", matcher.group(1));\r\nSystem.out.printf(\"\\nmatcher.group(2) value:%s\", matcher.group(2));\r\nSystem.out.printf(\"\\nmatcher.group(3) value:%s\", matcher.group(3));\r\nSystem.out.printf(\"\\nmatcher.group(4) value:%s\", matcher.group(4));<\/pre>\n
\u547d\u540d\u6355\u83b7\u7ec4<\/strong><\/div>\n

\u6bcf\u4e2a\u4ee5\u5de6\u62ec\u53f7\u5f00\u59cb\u7684\u6355\u83b7\u7ec4\uff0c\u90fd\u7d27\u8ddf\u7740 ?\uff0c\u800c\u540e\u624d\u662f\u6b63\u5219\u8868\u8fbe\u5f0f\u3002<\/p>\n

\u5bf9\u4e8e\u65f6\u95f4\u5b57\u7b26\u4e32\uff1a2017-04-25\uff0c\u8868\u8fbe\u5f0f\u5982\u4e0b:<\/p>\n

(?\\\\d{4})-(?(?\\\\d{2})-(?\\\\d{2}))<\/date><\/month><\/md><\/year><\/pre>\n

\u6709 4 \u4e2a\u547d\u540d\u7684\u6355\u83b7\u7ec4\uff0c\u5206\u522b\u662f:<\/p>\n\n\n\n\n\n\n\n\n\n
\u7f16\u53f7<\/th>\n\u540d\u79f0<\/th>\n\u6355\u83b7\u7ec4<\/th>\n\u5339\u914d<\/th>\n<\/tr>\n<\/thead>\n
0<\/td>\n0<\/td>\n(?\\d{4})-(?(?\\d{2})-(?\\d{2}))<\/td>\n2017-04-25<\/td>\n<\/tr>\n
1<\/td>\nyear<\/td>\n(?\\d{4})-<\/td>\n2017<\/td>\n<\/tr>\n
2<\/td>\nmd<\/td>\n(?(?\\d{2})-(?\\d{2}))<\/td>\n04-25<\/td>\n<\/tr>\n
3<\/td>\nmonth<\/td>\n(?\\d{2})<\/td>\n04<\/td>\n<\/tr>\n
4<\/td>\ndate<\/td>\n(?\\d{2})<\/td>\n25<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\u547d\u540d\u7684\u6355\u83b7\u7ec4\u540c\u6837\u4e5f\u53ef\u4ee5\u4f7f\u7528\u7f16\u53f7\u83b7\u53d6\u76f8\u5e94\u503c\u3002<\/p>\n

public static final String P_NAMED = \"(?\\\\d{4})-(?(?\\\\d{2})-(?\\\\d{2}))\";\r\npublic static final String DATE_STRING = \"2017-04-25\";\r\n \r\nPattern pattern = Pattern.compile(P_NAMED);\r\nMatcher matcher = pattern.matcher(DATE_STRING);\r\nmatcher.find();\r\nSystem.out.printf(\"\\n===========\u4f7f\u7528\u540d\u79f0\u83b7\u53d6=============\");\r\nSystem.out.printf(\"\\nmatcher.group(0) value:%s\", matcher.group(0));\r\nSystem.out.printf(\"\\n matcher.group('year') value:%s\", matcher.group(\"year\"));\r\nSystem.out.printf(\"\\nmatcher.group('md') value:%s\", matcher.group(\"md\"));\r\nSystem.out.printf(\"\\nmatcher.group('month') value:%s\", matcher.group(\"month\"));\r\nSystem.out.printf(\"\\nmatcher.group('date') value:%s\", matcher.group(\"date\"));\r\nmatcher.reset();\r\nSystem.out.printf(\"\\n===========\u4f7f\u7528\u7f16\u53f7\u83b7\u53d6=============\");\r\nmatcher.find();\r\nSystem.out.printf(\"\\nmatcher.group(0) value:%s\", matcher.group(0));\r\nSystem.out.printf(\"\\nmatcher.group(1) value:%s\", matcher.group(1));\r\nSystem.out.printf(\"\\nmatcher.group(2) value:%s\", matcher.group(2));\r\nSystem.out.printf(\"\\nmatcher.group(3) value:%s\", matcher.group(3));\r\nSystem.out.printf(\"\\nmatcher.group(4) value:%s\", matcher.group(4));<\/date><\/month><\/md><\/year><\/pre>\n
PS:\u975e\u6355\u83b7\u7ec4<\/strong><\/div>\n

\u5728\u5de6\u62ec\u53f7\u540e\u7d27\u8ddf ?:\uff0c\u800c\u540e\u518d\u52a0\u4e0a\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u6784\u6210\u975e\u6355\u83b7\u7ec4 (?:Expression)\u3002<\/p>\n

\u5bf9\u4e8e\u65f6\u95f4\u5b57\u7b26\u4e32\uff1a2017-04-25\uff0c\u8868\u8fbe\u5f0f\u5982\u4e0b:<\/p>\n

(?:\\\\d{4})-((\\\\d{2})-(\\\\d{2}))<\/pre>\n

\u8fd9\u4e2a\u6b63\u5219\u8868\u8fbe\u5f0f\u867d\u7136\u6709\u56db\u4e2a\u5de6\u62ec\u53f7\uff0c\u7406\u8bba\u4e0a\u6709 4 \u4e2a\u6355\u83b7\u7ec4\u3002\u4f46\u662f\u7b2c\u4e00\u7ec4 (?:\\d{4})\uff0c\u5176\u5b9e\u662f\u88ab\u5ffd\u7565\u7684\u3002\u5f53\u4f7f\u7528 matcher.group(4) \u65f6\uff0c\u7cfb\u7edf\u4f1a\u62a5\u9519\u3002<\/p>\n\n\n\n\n\n\n\n\n
\u7f16\u53f7<\/th>\n\u6355\u83b7\u7ec4<\/th>\n\u5339\u914d<\/th>\n<\/tr>\n<\/thead>\n
0<\/td>\n(\\d{4})-((\\d{2})-(\\d{2}))<\/td>\n2017-04-25<\/td>\n<\/tr>\n
1<\/td>\n((\\d{2})-(\\d{2}))<\/td>\n04-25<\/td>\n<\/tr>\n
2<\/td>\n(\\d{2})<\/td>\n04<\/td>\n<\/tr>\n
3<\/td>\n(\\d{2})<\/td>\n25<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n
\u603b\u7ed3<\/strong><\/div>\n
    \n
  1. \u666e\u901a\u6355\u83b7\u7ec4\u4f7f\u7528\u65b9\u4fbf\uff1b<\/li>\n
  2. \u547d\u540d\u6355\u83b7\u7ec4\u4f7f\u7528\u6e05\u6670\uff1b<\/li>\n
  3. \u975e\u6355\u83b7\u7ec4\u76ee\u524d\u5728\u9879\u76ee\u4e2d\u8fd8\u6ca1\u6709\u7528\u6b66\u4e4b\u5730\u3002<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"

    Java \u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u6355\u83b7\u7ec4\u5206\u4e3a\uff1a \u666e\u901a\u6355\u83b7\u7ec4(Expression) \u547d\u540d\u6355\u83b7\u7ec4(?Expression) […]<\/p>\n","protected":false},"author":1470,"featured_media":199454,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-199445","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/199445","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1470"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=199445"}],"version-history":[{"count":8,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/199445\/revisions"}],"predecessor-version":[{"id":199664,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/199445\/revisions\/199664"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/199454"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=199445"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=199445"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=199445"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}