{"id":145,"date":"2019-01-21T00:42:53","date_gmt":"2019-01-21T00:42:53","guid":{"rendered":"https:\/\/www.kindsonthegenius.com\/java\/?p=145"},"modified":"2020-08-06T09:54:32","modified_gmt":"2020-08-06T09:54:32","slug":"21-java-regular-expressions-2","status":"publish","type":"post","link":"https:\/\/www.kindsonthegenius.com\/java\/21-java-regular-expressions-2\/","title":{"rendered":"Java &#8211; Regular Expressions &#8211; 2"},"content":{"rendered":"<p>We have already covered the basics of regular expressions in <a href=\"https:\/\/www.kindsonthegenius.com\/java\/java-regular-expressions\/\">Part 1<\/a>.\u00a0 I recommend you review it. Now we are going to examine some more useful methods. We would also write the code to demonstrate it.<\/p>\n<p>Furthermore, <a href=\"#tv\">a video is provided at the end of the page<\/a>.<\/p>\n<p>&nbsp;<\/p>\n<p>We would cover the following topics<\/p>\n<ol>\n<li><a href=\"#t1\">start and end Methods<\/a><\/li>\n<li><a href=\"#t2\">Matches and lookingAt Methods<\/a><\/li>\n<li><a href=\"#t3\">replaceFirst and replaceAll Methods<\/a><\/li>\n<li><a href=\"#t4\">appendReplacement and appendTail Methods<\/a><\/li>\n<li><a href=\"#t5\">PatternSyntaxException Class Method<\/a><\/li>\n<li><a href=\"#t6\">Video Explanation<\/a><\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t1\">1. start and end Methods<\/strong><\/h4>\n<p>You use the start() method to find the starting index of a match in a given text. So if we have:<\/p>\n<p style=\"text-align: center;\">text = &#8220;The cup is on the floor&#8221;<\/p>\n<p>And we find find that cup matches a regular expression. Then the start() method would return 4. Because that is the starting index of cup in the text. Similarly, the end() method would return 6. This is because 6 is the end index of cup in the text. That is the index of letter p.<\/p>\n<p>Let&#8217;s take an example. We would write a program that counts the number of times the word cup appear in a string<\/p>\n<p>&nbsp;<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #888888;\">\/\/Program to count number of times<\/span>\r\n<span style=\"color: #888888;\">\/\/cup appears in a string<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">import<\/span> <span style=\"color: #0e84b5; font-weight: bold;\">java.util.regex.Matcher<\/span><span style=\"color: #333333;\">;<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">import<\/span> <span style=\"color: #0e84b5; font-weight: bold;\">java.util.regex.Pattern<\/span><span style=\"color: #333333;\">;<\/span>\r\n\r\n<span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #008800; font-weight: bold;\">class<\/span> <span style=\"color: #bb0066; font-weight: bold;\">RegexDemo2<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\r\n\t<span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #008800; font-weight: bold;\">static<\/span> <span style=\"color: #333399; font-weight: bold;\">void<\/span> <span style=\"color: #0066bb; font-weight: bold;\">main<\/span><span style=\"color: #333333;\">(<\/span>String<span style=\"color: #333333;\">[]<\/span> args<span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\t\tString text <span style=\"color: #333333;\">=<\/span> <span style=\"background-color: #fff0f0;\">\"This is blue cup. I need red cup\"<\/span><span style=\"color: #333333;\">;<\/span>\r\n\t\tString regex <span style=\"color: #333333;\">=<\/span> <span style=\"background-color: #fff0f0;\">\"\\\\bcup\\\\b\"<\/span><span style=\"color: #333333;\">;<\/span>\r\n\t\t\r\n\t\tPattern pt <span style=\"color: #333333;\">=<\/span> Pattern<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">compile<\/span><span style=\"color: #333333;\">(<\/span>regex<span style=\"color: #333333;\">);<\/span>\r\n\t\tMatcher mt <span style=\"color: #333333;\">=<\/span> pt<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">matcher<\/span><span style=\"color: #333333;\">(<\/span>text<span style=\"color: #333333;\">);<\/span>\r\n\t\t\r\n\t\t<span style=\"color: #333399; font-weight: bold;\">int<\/span> count <span style=\"color: #333333;\">=<\/span> <span style=\"color: #0000dd; font-weight: bold;\">0<\/span><span style=\"color: #333333;\">;<\/span>\r\n\t\t<span style=\"color: #008800; font-weight: bold;\">while<\/span><span style=\"color: #333333;\">(<\/span>mt<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">find<\/span><span style=\"color: #333333;\">())<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\t\t\tcount <span style=\"color: #333333;\">=<\/span> count <span style=\"color: #333333;\">+<\/span><span style=\"color: #0000dd; font-weight: bold;\">1<\/span><span style=\"color: #333333;\">;<\/span>\r\n\t\t\tSystem<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">out<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">println<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"Start index: \"<\/span> <span style=\"color: #333333;\">+<\/span> mt<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">start<\/span><span style=\"color: #333333;\">());<\/span>\r\n\t\t\tSystem<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">out<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">println<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"End index: \"<\/span> <span style=\"color: #333333;\">+<\/span> mt<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">end<\/span><span style=\"color: #333333;\">());<\/span>\r\n\t\t\tSystem<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">out<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">println<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"------------------------\"<\/span><span style=\"color: #333333;\">);<\/span>\r\n\t\t<span style=\"color: #333333;\">}<\/span>\t\t\r\n\t\tSystem<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">out<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">println<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"Total count: \"<\/span> <span style=\"color: #333333;\">+<\/span> count<span style=\"color: #333333;\">);<\/span>\r\n\t<span style=\"color: #333333;\">}<\/span>\r\n<span style=\"color: #333333;\">}<\/span>\r\n<\/pre>\n<p>Listing 1.0: Count how many times cup appears in a string<\/p>\n<p>&nbsp;<\/p>\n<p>I would suggest you run the code yourself. As such, you get used to it. Also try to change things a bit to see how it works. Maybe you can change cup to plate or something.<\/p>\n<p>Meanwhile, the output of the program is given below.<\/p>\n<p>&nbsp;<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">Start <span style=\"color: #997700; font-weight: bold;\">index:<\/span> <span style=\"color: #0000dd; font-weight: bold;\">13<\/span>\r\nEnd <span style=\"color: #997700; font-weight: bold;\">index:<\/span> <span style=\"color: #0000dd; font-weight: bold;\">16<\/span>\r\n<span style=\"color: #333333;\">------------------------<\/span>\r\nStart <span style=\"color: #997700; font-weight: bold;\">index:<\/span> <span style=\"color: #0000dd; font-weight: bold;\">29<\/span>\r\nEnd <span style=\"color: #997700; font-weight: bold;\">index:<\/span> <span style=\"color: #0000dd; font-weight: bold;\">32<\/span>\r\n<span style=\"color: #333333;\">------------------------<\/span>\r\nTotal <span style=\"color: #997700; font-weight: bold;\">count:<\/span> <span style=\"color: #0000dd; font-weight: bold;\">2<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>Also note that the program uses word boundaries \\b. This helps to ensure that the word is not inside another string. So if we have the word hiccup, then it would not match. Sure you will find it interesting to try it!<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t2\">2. Matches and lookingAt Methods<\/strong><\/h4>\n<p>The matches method as you already know matches an input text against a pattern. However, while matches tries to match the entire text, the lookingAt checks if the pattern matches the prefix of the text.<\/p>\n<p>Let&#8217;s take an example.<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #888888;\">\/\/Program to demonstrate difference<\/span>\r\n<span style=\"color: #888888;\">\/\/between matches() and lookingAt()<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">import<\/span> <span style=\"color: #0e84b5; font-weight: bold;\">java.util.regex.Matcher<\/span><span style=\"color: #333333;\">;<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">import<\/span> <span style=\"color: #0e84b5; font-weight: bold;\">java.util.regex.Pattern<\/span><span style=\"color: #333333;\">;<\/span>\r\n\r\n<span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #008800; font-weight: bold;\">class<\/span> <span style=\"color: #bb0066; font-weight: bold;\">RegexDemo2<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\r\n\t<span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #008800; font-weight: bold;\">static<\/span> <span style=\"color: #333399; font-weight: bold;\">void<\/span> <span style=\"color: #0066bb; font-weight: bold;\">main<\/span><span style=\"color: #333333;\">(<\/span>String<span style=\"color: #333333;\">[]<\/span> args<span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\t\tString text <span style=\"color: #333333;\">=<\/span> <span style=\"background-color: #fff0f0;\">\"big bigger bigest\"<\/span><span style=\"color: #333333;\">;<\/span>\r\n\t\tString regex <span style=\"color: #333333;\">=<\/span> <span style=\"background-color: #fff0f0;\">\"big\"<\/span><span style=\"color: #333333;\">;<\/span>\r\n\t\t\r\n\t\tPattern pt <span style=\"color: #333333;\">=<\/span> Pattern<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">compile<\/span><span style=\"color: #333333;\">(<\/span>regex<span style=\"color: #333333;\">);<\/span>\r\n\t\tMatcher mt <span style=\"color: #333333;\">=<\/span> pt<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">matcher<\/span><span style=\"color: #333333;\">(<\/span>text<span style=\"color: #333333;\">);<\/span>\r\n\t\t\r\n\t\tSystem<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">out<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">println<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"matches: \"<\/span> <span style=\"color: #333333;\">+<\/span> mt<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">matches<\/span><span style=\"color: #333333;\">());<\/span>\r\n\t\tSystem<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">out<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">println<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"lookingAt: \"<\/span> <span style=\"color: #333333;\">+<\/span> mt<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">lookingAt<\/span><span style=\"color: #333333;\">());<\/span>\r\n\t<span style=\"color: #333333;\">}<\/span>\r\n<span style=\"color: #333333;\">}<\/span>\r\n<\/pre>\n<p>Listing 1.1: Difference between matches() and lookingAt()<\/p>\n<p>&nbsp;<\/p>\n<p>If you run the code in Listing 1.1, you will have the result below:<\/p>\n<p>&nbsp;<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #997700; font-weight: bold;\">matches:<\/span> <span style=\"color: #008800; font-weight: bold;\">false<\/span>\r\n<span style=\"color: #997700; font-weight: bold;\">lookingAt:<\/span> <span style=\"color: #008800; font-weight: bold;\">true<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>Notice that for matches,\u00a0 it is false. This is because, matches tries to match the entire string. For lookingAt() however, it it is true. This is because the pattern &#8216;big&#8217; corresponds to the prefix of the text. That is the first part of the text is &#8216;big&#8217;. So it matches.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t3\">3. replaceFirst and replaceAll Methods<\/strong><\/h4>\n<p>You use these method to replace the text that matches a given regular expression. While replaceFirst() replaces only the first match, the replaceAll replaces all matches.<\/p>\n<p>Let&#8217;s take an example<\/p>\n<p>&nbsp;<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #888888;\">\/\/Program to demonstrate <\/span>\r\n<span style=\"color: #888888;\">\/\/replaceFirst() and replaceAll()<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">import<\/span> <span style=\"color: #0e84b5; font-weight: bold;\">java.util.regex.Matcher<\/span><span style=\"color: #333333;\">;<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">import<\/span> <span style=\"color: #0e84b5; font-weight: bold;\">java.util.regex.Pattern<\/span><span style=\"color: #333333;\">;<\/span>\r\n\r\n<span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #008800; font-weight: bold;\">class<\/span> <span style=\"color: #bb0066; font-weight: bold;\">RegexDemo2<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\r\n\t<span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #008800; font-weight: bold;\">static<\/span> <span style=\"color: #333399; font-weight: bold;\">void<\/span> <span style=\"color: #0066bb; font-weight: bold;\">main<\/span><span style=\"color: #333333;\">(<\/span>String<span style=\"color: #333333;\">[]<\/span> args<span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\t\tString text <span style=\"color: #333333;\">=<\/span> <span style=\"background-color: #fff0f0;\">\"big bigger bigest\"<\/span><span style=\"color: #333333;\">;<\/span>\r\n\t\tString regex <span style=\"color: #333333;\">=<\/span> <span style=\"background-color: #fff0f0;\">\"big\"<\/span><span style=\"color: #333333;\">;<\/span>\r\n\t\t\r\n\t\tPattern pt <span style=\"color: #333333;\">=<\/span> Pattern<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">compile<\/span><span style=\"color: #333333;\">(<\/span>regex<span style=\"color: #333333;\">);<\/span>\r\n\t\tMatcher mt <span style=\"color: #333333;\">=<\/span> pt<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">matcher<\/span><span style=\"color: #333333;\">(<\/span>text<span style=\"color: #333333;\">);<\/span>\r\n\t\t\r\n\t\tSystem<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">out<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">println<\/span><span style=\"color: #333333;\">(<\/span>mt<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">replaceFirst<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"small\"<\/span><span style=\"color: #333333;\">));<\/span>\r\n\t\tSystem<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">out<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">println<\/span><span style=\"color: #333333;\">(<\/span>mt<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">replaceAll<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"small\"<\/span><span style=\"color: #333333;\">));<\/span>\r\n\t<span style=\"color: #333333;\">}<\/span>\r\n<span style=\"color: #333333;\">}<\/span>\r\n<\/pre>\n<p>Listing 1.2: Program to illustrate replaceFirst() and replaceAll() Methods<\/p>\n<p>&nbsp;<\/p>\n<p>If you run the code, then you will have the result below:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">small bigger bigest\r\nsmall smallger smallest\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>Notice that in the first line of the output, only the first match is replaced. But in the second line, all the matches for &#8216;big&#8217; is replaced with small.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t1\">4. appendReplacement and appendTail Methods<\/strong><\/h4>\n<p>The <strong>appendReplacement()<\/strong> method does the following:<\/p>\n<ul>\n<li>It first reads characters from the input text, starting from the append position, and appends them to the given string buffer. It stops after is has read the lasts character preceding the previous match (the character at index start() -1)<\/li>\n<li>It then appends the given replacement string to the specified stringbuffer<\/li>\n<li>Finally, it sets the append position of the matcher to the index of the last matched character + 1 (that is to end())<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p>The <strong>appendTail()<\/strong> method does the following:<\/p>\n<p>It first reads characters from the input text, starting from the append position, and appends them to the given string buffer. (it implements a terminal append-and-replace step)<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t5\">5. PatternSyntaxException Class Method<\/strong><\/h4>\n<p>This is an exception that indicates the occurrence of a syntax error in\u00a0 a regular expression pattern. The following methods are provided in this class.<\/p>\n<p>&nbsp;<\/p>\n<table class=\"table table-bordered\" align=\"center\">\n<tbody>\n<tr style=\"background-color: #f7f6f3;\">\n<th>SN.<\/th>\n<th>Method and brief description<\/th>\n<\/tr>\n<tr>\n<td class=\"ts\">1<\/td>\n<td><b>getDescription()<\/b><\/p>\n<p>It retrieves the description of the error. Returns a string.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">2<\/td>\n<td><b>getIndex()<\/b><\/p>\n<p>It retrieves the error index. Returns an integer<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">3<\/td>\n<td><b>getPattern()<\/b><\/p>\n<p>It retrieves the erroneous regular expression pattern and returns a string.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">4<\/td>\n<td><b>getMessage()<\/b><\/p>\n<p>Returns a multi-line string that contains the description of the syntax error and its index, the erroneous regular expression, and a visual indication of the error index within the regular expression pattern.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<h4><a href=\"https:\/\/youtu.be\/sCuOJ8uadOg\"><strong id=\"t6\">6. Video Explanation<\/strong><\/a><\/h4>\n<p><iframe loading=\"lazy\" src=\"https:\/\/www.youtube.com\/embed\/sCuOJ8uadOg\" width=\"100%\" height=\"350\" frameborder=\"0\" allowfullscreen=\"allowfullscreen\"><\/iframe><\/p>\n","protected":false},"excerpt":{"rendered":"<p>We have already covered the basics of regular expressions in Part 1.\u00a0 I recommend you review it. Now we are going to examine some more &hellip; <\/p>\n","protected":false},"author":395,"featured_media":146,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[2],"tags":[],"class_list":["post-145","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java-tutorial"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Java - Regular Expressions - 2 - Java Tutorials<\/title>\n<meta name=\"description\" content=\"In this tutorial, we learn more about regular expressions. We cover matches and lookingAt. Also replaceAll and replaceFirst methods\" \/>\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.kindsonthegenius.com\/java\/21-java-regular-expressions-2\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Java - Regular Expressions - 2 - Java Tutorials\" \/>\n<meta property=\"og:description\" content=\"In this tutorial, we learn more about regular expressions. We cover matches and lookingAt. Also replaceAll and replaceFirst methods\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.kindsonthegenius.com\/java\/21-java-regular-expressions-2\/\" \/>\n<meta property=\"og:site_name\" content=\"Java Tutorials\" \/>\n<meta property=\"article:published_time\" content=\"2019-01-21T00:42:53+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-08-06T09:54:32+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.kindsonthegenius.com\/java\/wp-content\/uploads\/sites\/9\/2019\/01\/Regular-Expressions-in-Java-Part-2.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"954\" \/>\n\t<meta property=\"og:image:height\" content=\"518\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"kindsonthegenius\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"kindsonthegenius\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/21-java-regular-expressions-2\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/21-java-regular-expressions-2\\\/\"},\"author\":{\"name\":\"kindsonthegenius\",\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/#\\\/schema\\\/person\\\/63a68934672db675ff0cd80d066510c2\"},\"headline\":\"Java &#8211; Regular Expressions &#8211; 2\",\"datePublished\":\"2019-01-21T00:42:53+00:00\",\"dateModified\":\"2020-08-06T09:54:32+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/21-java-regular-expressions-2\\\/\"},\"wordCount\":718,\"commentCount\":2,\"image\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/21-java-regular-expressions-2\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/wp-content\\\/uploads\\\/sites\\\/9\\\/2019\\\/01\\\/Regular-Expressions-in-Java-Part-2.jpg\",\"articleSection\":[\"Java Tutorial\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/21-java-regular-expressions-2\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/21-java-regular-expressions-2\\\/\",\"url\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/21-java-regular-expressions-2\\\/\",\"name\":\"Java - Regular Expressions - 2 - Java Tutorials\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/21-java-regular-expressions-2\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/21-java-regular-expressions-2\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/wp-content\\\/uploads\\\/sites\\\/9\\\/2019\\\/01\\\/Regular-Expressions-in-Java-Part-2.jpg\",\"datePublished\":\"2019-01-21T00:42:53+00:00\",\"dateModified\":\"2020-08-06T09:54:32+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/#\\\/schema\\\/person\\\/63a68934672db675ff0cd80d066510c2\"},\"description\":\"In this tutorial, we learn more about regular expressions. We cover matches and lookingAt. Also replaceAll and replaceFirst methods\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/21-java-regular-expressions-2\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/21-java-regular-expressions-2\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/21-java-regular-expressions-2\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/wp-content\\\/uploads\\\/sites\\\/9\\\/2019\\\/01\\\/Regular-Expressions-in-Java-Part-2.jpg\",\"contentUrl\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/wp-content\\\/uploads\\\/sites\\\/9\\\/2019\\\/01\\\/Regular-Expressions-in-Java-Part-2.jpg\",\"width\":954,\"height\":518,\"caption\":\"Regular Expressions in Java\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/21-java-regular-expressions-2\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java &#8211; Regular Expressions &#8211; 2\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/#website\",\"url\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/\",\"name\":\"Java Tutorials\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/#\\\/schema\\\/person\\\/63a68934672db675ff0cd80d066510c2\",\"name\":\"kindsonthegenius\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/3079a7f663b02e801d03cd075852a037af36bd179b5fbcd0603bae3dd7833a9b?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/3079a7f663b02e801d03cd075852a037af36bd179b5fbcd0603bae3dd7833a9b?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/3079a7f663b02e801d03cd075852a037af36bd179b5fbcd0603bae3dd7833a9b?s=96&d=mm&r=g\",\"caption\":\"kindsonthegenius\"},\"url\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/author\\\/kindsonthegenius-2\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Java - Regular Expressions - 2 - Java Tutorials","description":"In this tutorial, we learn more about regular expressions. We cover matches and lookingAt. Also replaceAll and replaceFirst methods","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.kindsonthegenius.com\/java\/21-java-regular-expressions-2\/","og_locale":"en_US","og_type":"article","og_title":"Java - Regular Expressions - 2 - Java Tutorials","og_description":"In this tutorial, we learn more about regular expressions. We cover matches and lookingAt. Also replaceAll and replaceFirst methods","og_url":"https:\/\/www.kindsonthegenius.com\/java\/21-java-regular-expressions-2\/","og_site_name":"Java Tutorials","article_published_time":"2019-01-21T00:42:53+00:00","article_modified_time":"2020-08-06T09:54:32+00:00","og_image":[{"width":954,"height":518,"url":"https:\/\/www.kindsonthegenius.com\/java\/wp-content\/uploads\/sites\/9\/2019\/01\/Regular-Expressions-in-Java-Part-2.jpg","type":"image\/jpeg"}],"author":"kindsonthegenius","twitter_card":"summary_large_image","twitter_misc":{"Written by":"kindsonthegenius","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.kindsonthegenius.com\/java\/21-java-regular-expressions-2\/#article","isPartOf":{"@id":"https:\/\/www.kindsonthegenius.com\/java\/21-java-regular-expressions-2\/"},"author":{"name":"kindsonthegenius","@id":"https:\/\/www.kindsonthegenius.com\/java\/#\/schema\/person\/63a68934672db675ff0cd80d066510c2"},"headline":"Java &#8211; Regular Expressions &#8211; 2","datePublished":"2019-01-21T00:42:53+00:00","dateModified":"2020-08-06T09:54:32+00:00","mainEntityOfPage":{"@id":"https:\/\/www.kindsonthegenius.com\/java\/21-java-regular-expressions-2\/"},"wordCount":718,"commentCount":2,"image":{"@id":"https:\/\/www.kindsonthegenius.com\/java\/21-java-regular-expressions-2\/#primaryimage"},"thumbnailUrl":"https:\/\/www.kindsonthegenius.com\/java\/wp-content\/uploads\/sites\/9\/2019\/01\/Regular-Expressions-in-Java-Part-2.jpg","articleSection":["Java Tutorial"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.kindsonthegenius.com\/java\/21-java-regular-expressions-2\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.kindsonthegenius.com\/java\/21-java-regular-expressions-2\/","url":"https:\/\/www.kindsonthegenius.com\/java\/21-java-regular-expressions-2\/","name":"Java - Regular Expressions - 2 - Java Tutorials","isPartOf":{"@id":"https:\/\/www.kindsonthegenius.com\/java\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.kindsonthegenius.com\/java\/21-java-regular-expressions-2\/#primaryimage"},"image":{"@id":"https:\/\/www.kindsonthegenius.com\/java\/21-java-regular-expressions-2\/#primaryimage"},"thumbnailUrl":"https:\/\/www.kindsonthegenius.com\/java\/wp-content\/uploads\/sites\/9\/2019\/01\/Regular-Expressions-in-Java-Part-2.jpg","datePublished":"2019-01-21T00:42:53+00:00","dateModified":"2020-08-06T09:54:32+00:00","author":{"@id":"https:\/\/www.kindsonthegenius.com\/java\/#\/schema\/person\/63a68934672db675ff0cd80d066510c2"},"description":"In this tutorial, we learn more about regular expressions. We cover matches and lookingAt. Also replaceAll and replaceFirst methods","breadcrumb":{"@id":"https:\/\/www.kindsonthegenius.com\/java\/21-java-regular-expressions-2\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.kindsonthegenius.com\/java\/21-java-regular-expressions-2\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.kindsonthegenius.com\/java\/21-java-regular-expressions-2\/#primaryimage","url":"https:\/\/www.kindsonthegenius.com\/java\/wp-content\/uploads\/sites\/9\/2019\/01\/Regular-Expressions-in-Java-Part-2.jpg","contentUrl":"https:\/\/www.kindsonthegenius.com\/java\/wp-content\/uploads\/sites\/9\/2019\/01\/Regular-Expressions-in-Java-Part-2.jpg","width":954,"height":518,"caption":"Regular Expressions in Java"},{"@type":"BreadcrumbList","@id":"https:\/\/www.kindsonthegenius.com\/java\/21-java-regular-expressions-2\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.kindsonthegenius.com\/java\/"},{"@type":"ListItem","position":2,"name":"Java &#8211; Regular Expressions &#8211; 2"}]},{"@type":"WebSite","@id":"https:\/\/www.kindsonthegenius.com\/java\/#website","url":"https:\/\/www.kindsonthegenius.com\/java\/","name":"Java Tutorials","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.kindsonthegenius.com\/java\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.kindsonthegenius.com\/java\/#\/schema\/person\/63a68934672db675ff0cd80d066510c2","name":"kindsonthegenius","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/3079a7f663b02e801d03cd075852a037af36bd179b5fbcd0603bae3dd7833a9b?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/3079a7f663b02e801d03cd075852a037af36bd179b5fbcd0603bae3dd7833a9b?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/3079a7f663b02e801d03cd075852a037af36bd179b5fbcd0603bae3dd7833a9b?s=96&d=mm&r=g","caption":"kindsonthegenius"},"url":"https:\/\/www.kindsonthegenius.com\/java\/author\/kindsonthegenius-2\/"}]}},"_links":{"self":[{"href":"https:\/\/www.kindsonthegenius.com\/java\/wp-json\/wp\/v2\/posts\/145","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kindsonthegenius.com\/java\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kindsonthegenius.com\/java\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/java\/wp-json\/wp\/v2\/users\/395"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/java\/wp-json\/wp\/v2\/comments?post=145"}],"version-history":[{"count":2,"href":"https:\/\/www.kindsonthegenius.com\/java\/wp-json\/wp\/v2\/posts\/145\/revisions"}],"predecessor-version":[{"id":210,"href":"https:\/\/www.kindsonthegenius.com\/java\/wp-json\/wp\/v2\/posts\/145\/revisions\/210"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/java\/wp-json\/wp\/v2\/media\/146"}],"wp:attachment":[{"href":"https:\/\/www.kindsonthegenius.com\/java\/wp-json\/wp\/v2\/media?parent=145"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/java\/wp-json\/wp\/v2\/categories?post=145"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/java\/wp-json\/wp\/v2\/tags?post=145"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}