{"id":523,"date":"2023-09-16T15:39:10","date_gmt":"2023-09-16T07:39:10","guid":{"rendered":""},"modified":"2023-09-17T00:42:13","modified_gmt":"2023-09-16T16:42:13","slug":"java%e7%ae%80%e5%8d%95%e7%9a%84%e6%ad%a3%e5%88%99%e8%a1%a8%e8%be%be%e5%bc%8f%e5%8c%b9%e9%85%8d%e6%93%8d%e4%bd%9c","status":"publish","type":"post","link":"https:\/\/mushiming.com\/523.html","title":{"rendered":"Java\u7b80\u5355\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u64cd\u4f5c"},"content":{"rendered":"

\n <\/path> \n<\/svg> <\/p>\n

\u7b80\u5355\u5730\u5229\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u8fdb\u884c\u5339\u914d\u64cd\u4f5c<\/strong><\/h2>\n
\u8981\u660e\u786e\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u7528\u6765\u65b9\u4fbf\u5730\u5339\u914d\u5b57\u7b26\u4e32\u7684\n<\/code><\/pre>\n

1. \u9996\u5148\u8981\u521b\u5efa\u4e00\u4e2a\u5339\u914d\u6a21\u5f0f<\/h4>\n

\u4e00\u4e2a\u5339\u914d\u6a21\u5f0f\u5c31\u662f\u4e00\u4e2a\u5bf9\u8c61\uff0c\u8fd9\u4e2a\u5bf9\u8c61\u7531Pattern\u7c7b\u6765\u5e2e\u6211\u4eec\u9020\u8981\u6ce8\u610fPattern\u7684\u6784\u9020\u65b9\u6cd5\u662f\u79c1\u6709\u7684\uff0c\u6240\u4ee5\u6ca1\u529e\u6cd5\u76f4\u63a5new\uff0c\u4f46\u662f\u53ef\u4ee5\u901a\u8fc7\u5b83\u7684\u4e00\u4e2a\u9759\u6001\u65b9\u6cd5compile()\u6765\u521b\u5efa\u3002<\/h6>\n
Pattern\u91cc\u8fd8\u6709\u4e00\u4e2amatcher\u65b9\u6cd5\uff0c\u8fd4\u56de\u4e00\u4e2aMatcher\u7684\u5bf9\u8c61\uff0c\u8be5\u65b9\u6cd5\u7684\u53c2\u6570\u662f\u4e00\u4e2a\u4f60\u60f3\u5339\u914d\u7684\u90a3\u4e2a\u5b57\u7b26\u4e32\u3002\u8be6\u7ec6\u89c1\u4e0b\u6587Matcher\u3002<\/h6>\n
\u521b\u5efa\u4e00\u4e2a\u5339\u914d\u6570\u5b57\u5b57\u7b26\u7684\u6a21\u5f0f\uff1a<\/h6>\n
Pattern<\/span> pattern =<\/span> Pattern<\/span>.<\/span>compile<\/span>(<\/span>\"\\\\d+\"<\/span>)<\/span>;<\/span>\/\/\u521b\u5efa\u5339\u914d\u6570\u5b57\u5b57\u7b26\u7684\u6a21\u5f0f<\/span>\n<\/code><\/pre>\n
\u5339\u914d\u6a21\u5f0f\u7684\u610f\u601d\u5c31\u662f\u4e00\u4e2a\u6a21\u677f\uff0c\u7528\u51e0\u4e2a\u7b26\u53f7\u8868\u793a\u4f60\u60f3\u5339\u914d\u7684\u4e1c\u897f\uff0c\u5c31\u53eb\u505a\u5339\u914d\u6a21\u5f0f\u3002\n\t\t\n\t\u4f8b\u5982\uff1a\n\t\t\n\t\\\\d+\uff0c\u4ee3\u8868\u5339\u914d\u4e00\u4e2a\u6216\u591a\u4e2a\u5b57\u7b26\uff0c\u5176\u4e2d\u2018\\d\u2019\u4ee3\u8868\u5339\u914d\u6570\u5b57\uff0c\u7b49\u6548\u4e8e[0-9]\u3002\n\t\n\t\u53cd\u659c\u6760\u7528\u6765\u8f6c\u4e49\uff0c\u56e0\u4e3a\\d\u672c\u8eab\u6709\u7279\u6b8a\u7684\u542b\u4e49\uff0c\u6240\u4ee5\u8ba9\u6700\u524d\u9762\n\t\u7684\u90a3\u4e2a\u53cd\u659c\u6760\u5c06\u7b2c\u4e8c\u4e2a\u53cd\u659c\u6760\u53d8\u4e3a\u4e00\u4e2a\u5b57\u7b26'\\'\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u4f7f\\d\u662f\u4e00\u4e2a\u7b26\u53f7\u4e86\n\t\n\t\u2019+\u2018\u4ee3\u8868\u5339\u914d\u524d\u9762\u7684\u5b57\u7b26\u6216\u5b50\u8868\u8fbe\u5f0f\u4e00\u6b21\u6216\u591a\u6b21\uff0c\u6bd4\u5982\uff1a\"wsx+\"\u53ef\u4ee5\u548c\n\t\"wsxw\"\u3001\"wsx\"\u3001\"swsxx\"\u7b49\u7b49\u5339\u914d\uff0c\u4e0d\u53ef\u4ee5\u548c\"ws\"\u3001\"sxw\"\u7b49\u7b49\u5339\u914d\n<\/code><\/pre>\n

2. \u63a5\u4e0b\u6765\u5c31\u662f\u9009\u62e9\u5339\u914d\u7684\u5bf9\u8c61(\u5b57\u7b26\u4e32)<\/h4>\n
\u6211\u4eec\u4f1a\u7528\u5230\u5de5\u5177\u7c7bMatcher\uff0c\u4e2d\u6587\u5c31\u662f\u5339\u914d\u7684\u610f\u601d\u3002\n\nMatcher\u7684\u4e00\u4e2a\u5b9e\u4f8b\u4ee3\u8868\u4e00\u4e2a\u7528\u6765\u64cd\u4f5c\u5339\u914d\u5b57\u7b26\u4e32\u7684\u5bf9\u8c61\u3002\n\nMatcher\u5e38\u7528\u7684\u65b9\u6cd5\u6709find()\u3001start()\u3001end()\n\n\t<1> find()\u7528\u4e8e\u67e5\u627e\u662f\u5426\u6709\u60f3\u627e\u7684\u90a3\u4e2a\u5b57\u7b26\uff0c\u6709\u7684\u8bdd\u8fd4\u56detrue\n\t<2> start()\u8fd4\u56de\u4e0a\u4e00\u4e2a\u5339\u914d\u9879\u7684\u8d77\u59cb\u7d22\u5f15\n\t<3> end()\u8fd4\u56de\u4e0a\u4e00\u4e2a\u5339\u914d\u9879\u7684\u672b\u5c3e\u7d22\u5f15\n\t\n\t\u8fd9\u4e09\u79cd\u65b9\u6cd5\u53ef\u4ee5\u4e0e\u5faa\u73af\u7ed3\u5408\u8d77\u6765\uff0c\u6ca1\u6709\u5339\u914d\u7684\u8bdd\u5b83\u4f1a\u81ea\u52a8\u8fed\u4ee3\uff0c\u7ee7\u7eed\u5f80\u4e0b\u627e\uff0c\n\u76f4\u81f3\u5339\u914d\u7ed3\u675f\u3002\n<\/code><\/pre>\n

3. \u63a5\u4e0b\u6765\u5c31\u662f\u53bb\u6267\u884c\u5339\u914d\u64cd\u4f5c<\/h4>\n

\u7531\u4e8efind()\u8fd4\u56de\u4e00\u4e2a\u5e03\u5c14\u503c\uff0c\u6240\u4ee5\u6211\u4eec\u53ef\u4ee5\u628a\u5b83\u76f4\u63a5\u5f53\u4f5cwhile\u7684\u6761\u4ef6\u3002<\/h6>\n

\u4e0b\u9762\u662f\u4e00\u4e2a\u4ece\u5b57\u7b26\u4e32\u4e2d\u83b7\u5f97\u6570\u5b57\u7684\u4e00\u4e2a\u5b9e\u4f8b\uff1a<\/p>\n

String<\/span> str =<\/span> \"cheems123cheems456cheems789\"<\/span>;<\/span>\n\n\n\/\/1.\u521b\u5efa\u5339\u914d\u6a21\u5f0f<\/span>\nPattern<\/span> pattern =<\/span> Pattern<\/span>.<\/span>compile<\/span>(<\/span>\"\\\\d+\"<\/span>)<\/span>;<\/span>\/\/\u5339\u914d\u4e00\u4e2a\u6216\u591a\u4e2a\u6570\u5b57\u5b57\u7b26<\/span>\n\n\/\/2.\u9009\u62e9\u5339\u914d\u5bf9\u8c61<\/span>\nMatcher<\/span> matcher =<\/span> pattern.<\/span>matcher<\/span>(<\/span>str)<\/span>;<\/span>\n\/\/\u4e0e\u8c01\u5339\u914d\uff1f\u4e0e\u53c2\u6570\u5b57\u7b26\u4e32str\u5339\u914d<\/span>\n\n\nint<\/span> count =<\/span> 0<\/span>;<\/span>\nwhile<\/span> (<\/span>matcher.<\/span>find<\/span>(<\/span>)<\/span>)<\/span>\/\/matcher.find()\u7528\u4e8e\u67e5\u627e\u662f\u5426\u6709\u8fd9\u4e2a\u5b57\u7b26\uff0c\u6709\u7684\u8bdd\u8fd4\u56detrue<\/span>\n{ \n   <\/span>\n\t  System<\/span>.<\/span>out.<\/span>println<\/span>(<\/span>\"\u7b2c\"<\/span> +<\/span> (<\/span>++<\/span>count)<\/span> +<\/span> \"\u6b21\u627e\u5230\"<\/span>)<\/span>;<\/span>\n\t  \/\/start()\u8fd4\u56de\u4e0a\u4e00\u4e2a\u5339\u914d\u9879\u7684\u8d77\u59cb\u7d22\u5f15<\/span>\n\t  \/\/end()\u8fd4\u56de\u4e0a\u4e00\u4e2a\u5339\u914d\u9879\u7684\u672b\u5c3e\u7d22\u5f15\u3002<\/span>\n  \t  System<\/span>.<\/span>out.<\/span>println<\/span>(<\/span>str.<\/span>substring<\/span>(<\/span>matcher.<\/span>start<\/span>(<\/span>)<\/span>,<\/span>matcher.<\/span>end<\/span>(<\/span>)<\/span>)<\/span>)<\/span>;<\/span>\n}<\/span>\n<\/code><\/pre>\n

\u7ed3\u679c\uff1a
\"Java\u7b80\u5355\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u64cd\u4f5c<\/p>\n

\u7531\u4e8e\u662f\u521d\u5b66\u8005\uff0c\u5982\u6709\u9519\u8bef\u8bf7\u5927\u529b\u6307\u51fa\uff0c\u6211\u4e00\u5b9a\u865a\u5fc3\u63a5\u53d7<\/p>\n

\u53c2\u8003\uff1aJava\u7b80\u5355\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u64cd\u4f5c<\/p>\n","protected":false},"excerpt":{"rendered":"Java\u7b80\u5355\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u64cd\u4f5c\u7b80\u5355\u5730\u5229\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u8fdb\u884c\u5339\u914d\u64cd\u4f5c\u8981\u660e\u786e\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u7528\u6765\u65b9\u4fbf\u5730\u5339\u914d\u5b57\u7b26\u4e32\u76841.\u9996\u5148\u8981\u521b\u5efa\u4e00...","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\/523"}],"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=523"}],"version-history":[{"count":0,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/523\/revisions"}],"wp:attachment":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/media?parent=523"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/categories?post=523"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/tags?post=523"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}