{"id":13679,"date":"2024-03-15T07:38:28","date_gmt":"2024-03-15T07:38:28","guid":{"rendered":"https:\/\/www.silicloud.com\/blog\/what-functions-does-the-matches-method-in-java-have\/"},"modified":"2025-08-05T21:56:18","modified_gmt":"2025-08-05T21:56:18","slug":"what-functions-does-the-matches-method-in-java-have","status":"publish","type":"post","link":"https:\/\/www.silicloud.com\/blog\/what-functions-does-the-matches-method-in-java-have\/","title":{"rendered":"Java matches Method Guide"},"content":{"rendered":"<p>The matches function in Java is used to determine if a string matches a specified regular expression.<\/p>\n<p>The functionality of the matches function includes:<\/p>\n<ol>\n<li>Check if a string completely matches a regular expression: return true if the string matches the regular expression exactly, otherwise return false.<\/li>\n<li>Check if a string contains a substring that matches a regular expression: return true if there is a matching substring, otherwise return false.<\/li>\n<li>Determine if a string follows a specified format by using regular expressions to define the format and the matches function to verify compliance.<\/li>\n<li>Validate the input&#8217;s legitimacy: Regular expressions can be defined as rules used to verify the legality of user input, and the matches function can determine if the user input adheres to those rules.<\/li>\n<\/ol>\n<p>It is important to note that the matches function can only determine if the entire string matches the regular expression. If there is a substring within the string that matches the regular expression but the entire string does not, the matches function will still return false.<\/p>\n<p>For example, the following code uses the matches function to determine if a string is a valid phone number:<\/p>\n<pre class=\"post-pre\"><code><span class=\"hljs-type\">String<\/span> <span class=\"hljs-variable\">phoneNumber<\/span> <span class=\"hljs-operator\">=<\/span> <span class=\"hljs-string\">\"13812345678\"<\/span>;\r\n<span class=\"hljs-keyword\">if<\/span> (phoneNumber.matches(<span class=\"hljs-string\">\"1[3456789]\\\\d{9}\"<\/span>)) {\r\n    System.out.println(<span class=\"hljs-string\">\"\u5408\u6cd5\u7684\u624b\u673a\u53f7\u7801\"<\/span>);\r\n} <span class=\"hljs-keyword\">else<\/span> {\r\n    System.out.println(<span class=\"hljs-string\">\"\u975e\u6cd5\u7684\u624b\u673a\u53f7\u7801\"<\/span>);\r\n}\r\n<\/code><\/pre>\n<p>The output is &#8220;valid phone number&#8221;.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The matches function in Java is used to determine if a string matches a specified regular expression. The functionality of the matches function includes: Check if a string completely matches a regular expression: return true if the string matches the regular expression exactly, otherwise return false. Check if a string contains a substring that matches [&hellip;]<\/p>\n","protected":false},"author":13,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[1],"tags":[18273,1990,1992,18274,184],"class_list":["post-13679","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-java-matches","tag-java-regex","tag-pattern-matching","tag-regex-java","tag-string-methods"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v21.5 (Yoast SEO v21.5) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Java matches Method Guide - Blog - Silicon Cloud<\/title>\n<meta name=\"description\" content=\"Learn how Java&#039;s matches() method checks string patterns with regex. Complete functionality explained with examples.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.silicloud.com\/blog\/what-functions-does-the-matches-method-in-java-have\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Java matches Method Guide\" \/>\n<meta property=\"og:description\" content=\"Learn how Java&#039;s matches() method checks string patterns with regex. Complete functionality explained with examples.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.silicloud.com\/blog\/what-functions-does-the-matches-method-in-java-have\/\" \/>\n<meta property=\"og:site_name\" content=\"Blog - Silicon Cloud\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/SiliCloudGlobal\/\" \/>\n<meta property=\"article:published_time\" content=\"2024-03-15T07:38:28+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-08-05T21:56:18+00:00\" \/>\n<meta name=\"author\" content=\"Isabella Edwards\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@SiliCloudGlobal\" \/>\n<meta name=\"twitter:site\" content=\"@SiliCloudGlobal\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Isabella Edwards\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/what-functions-does-the-matches-method-in-java-have\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/what-functions-does-the-matches-method-in-java-have\/\"},\"author\":{\"name\":\"Isabella Edwards\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/5579144e23c225c8188167f3e3f888dd\"},\"headline\":\"Java matches Method Guide\",\"datePublished\":\"2024-03-15T07:38:28+00:00\",\"dateModified\":\"2025-08-05T21:56:18+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/what-functions-does-the-matches-method-in-java-have\/\"},\"wordCount\":201,\"publisher\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\"},\"keywords\":[\"Java matches\",\"Java regex\",\"pattern matching\",\"regex Java\",\"string methods\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/what-functions-does-the-matches-method-in-java-have\/\",\"url\":\"https:\/\/www.silicloud.com\/blog\/what-functions-does-the-matches-method-in-java-have\/\",\"name\":\"Java matches Method Guide - Blog - Silicon Cloud\",\"isPartOf\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#website\"},\"datePublished\":\"2024-03-15T07:38:28+00:00\",\"dateModified\":\"2025-08-05T21:56:18+00:00\",\"description\":\"Learn how Java's matches() method checks string patterns with regex. Complete functionality explained with examples.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/what-functions-does-the-matches-method-in-java-have\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.silicloud.com\/blog\/what-functions-does-the-matches-method-in-java-have\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/what-functions-does-the-matches-method-in-java-have\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.silicloud.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java matches Method Guide\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#website\",\"url\":\"https:\/\/www.silicloud.com\/blog\/\",\"name\":\"Silicon Cloud Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\"},\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\",\"name\":\"Silicon Cloud Blog\",\"url\":\"https:\/\/www.silicloud.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png\",\"contentUrl\":\"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png\",\"width\":1024,\"height\":1024,\"caption\":\"Silicon Cloud Blog\"},\"image\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/SiliCloudGlobal\/\",\"https:\/\/twitter.com\/SiliCloudGlobal\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/5579144e23c225c8188167f3e3f888dd\",\"name\":\"Isabella Edwards\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/d4d4dec47f553ac7961d9fa4cc9bdcdcf5b7ce5106594330b6d25c5694fdbaec?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/d4d4dec47f553ac7961d9fa4cc9bdcdcf5b7ce5106594330b6d25c5694fdbaec?s=96&d=mm&r=g\",\"caption\":\"Isabella Edwards\"},\"url\":\"https:\/\/www.silicloud.com\/blog\/author\/isabellaedwards\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Java matches Method Guide - Blog - Silicon Cloud","description":"Learn how Java's matches() method checks string patterns with regex. Complete functionality explained with examples.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.silicloud.com\/blog\/what-functions-does-the-matches-method-in-java-have\/","og_locale":"en_US","og_type":"article","og_title":"Java matches Method Guide","og_description":"Learn how Java's matches() method checks string patterns with regex. Complete functionality explained with examples.","og_url":"https:\/\/www.silicloud.com\/blog\/what-functions-does-the-matches-method-in-java-have\/","og_site_name":"Blog - Silicon Cloud","article_publisher":"https:\/\/www.facebook.com\/SiliCloudGlobal\/","article_published_time":"2024-03-15T07:38:28+00:00","article_modified_time":"2025-08-05T21:56:18+00:00","author":"Isabella Edwards","twitter_card":"summary_large_image","twitter_creator":"@SiliCloudGlobal","twitter_site":"@SiliCloudGlobal","twitter_misc":{"Written by":"Isabella Edwards","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.silicloud.com\/blog\/what-functions-does-the-matches-method-in-java-have\/#article","isPartOf":{"@id":"https:\/\/www.silicloud.com\/blog\/what-functions-does-the-matches-method-in-java-have\/"},"author":{"name":"Isabella Edwards","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/5579144e23c225c8188167f3e3f888dd"},"headline":"Java matches Method Guide","datePublished":"2024-03-15T07:38:28+00:00","dateModified":"2025-08-05T21:56:18+00:00","mainEntityOfPage":{"@id":"https:\/\/www.silicloud.com\/blog\/what-functions-does-the-matches-method-in-java-have\/"},"wordCount":201,"publisher":{"@id":"https:\/\/www.silicloud.com\/blog\/#organization"},"keywords":["Java matches","Java regex","pattern matching","regex Java","string methods"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.silicloud.com\/blog\/what-functions-does-the-matches-method-in-java-have\/","url":"https:\/\/www.silicloud.com\/blog\/what-functions-does-the-matches-method-in-java-have\/","name":"Java matches Method Guide - Blog - Silicon Cloud","isPartOf":{"@id":"https:\/\/www.silicloud.com\/blog\/#website"},"datePublished":"2024-03-15T07:38:28+00:00","dateModified":"2025-08-05T21:56:18+00:00","description":"Learn how Java's matches() method checks string patterns with regex. Complete functionality explained with examples.","breadcrumb":{"@id":"https:\/\/www.silicloud.com\/blog\/what-functions-does-the-matches-method-in-java-have\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.silicloud.com\/blog\/what-functions-does-the-matches-method-in-java-have\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.silicloud.com\/blog\/what-functions-does-the-matches-method-in-java-have\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.silicloud.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Java matches Method Guide"}]},{"@type":"WebSite","@id":"https:\/\/www.silicloud.com\/blog\/#website","url":"https:\/\/www.silicloud.com\/blog\/","name":"Silicon Cloud Blog","description":"","publisher":{"@id":"https:\/\/www.silicloud.com\/blog\/#organization"},"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.silicloud.com\/blog\/#organization","name":"Silicon Cloud Blog","url":"https:\/\/www.silicloud.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png","contentUrl":"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png","width":1024,"height":1024,"caption":"Silicon Cloud Blog"},"image":{"@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/SiliCloudGlobal\/","https:\/\/twitter.com\/SiliCloudGlobal"]},{"@type":"Person","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/5579144e23c225c8188167f3e3f888dd","name":"Isabella Edwards","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/d4d4dec47f553ac7961d9fa4cc9bdcdcf5b7ce5106594330b6d25c5694fdbaec?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/d4d4dec47f553ac7961d9fa4cc9bdcdcf5b7ce5106594330b6d25c5694fdbaec?s=96&d=mm&r=g","caption":"Isabella Edwards"},"url":"https:\/\/www.silicloud.com\/blog\/author\/isabellaedwards\/"}]}},"_links":{"self":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/13679","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/users\/13"}],"replies":[{"embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/comments?post=13679"}],"version-history":[{"count":2,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/13679\/revisions"}],"predecessor-version":[{"id":157680,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/13679\/revisions\/157680"}],"wp:attachment":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/media?parent=13679"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/categories?post=13679"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/tags?post=13679"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}