{"id":4638,"date":"2012-05-12T09:23:40","date_gmt":"2012-05-12T14:23:40","guid":{"rendered":"http:\/\/jianmingli.com\/wp\/?p=4638"},"modified":"2013-02-05T10:23:04","modified_gmt":"2013-02-05T15:23:04","slug":"java-regex","status":"publish","type":"post","link":"https:\/\/jianmingli.com\/wp\/?p=4638","title":{"rendered":"Java RegEx"},"content":{"rendered":"<div class='toc wptoc'>\n<h2>Contents<\/h2>\n<ol class='toc-odd level-1'>\n\t<li>\n\t\t<a href=\"#Test_Harness\">Test Harness<\/a>\n\t<\/li>\n\t<li>\n\t\t<a href=\"#String_Literals\">String Literals<\/a>\n\t<\/li>\n\t<li>\n\t\t<a href=\"#Metacharaters\">Metacharaters<\/a>\n\t<\/li>\n\t<li>\n\t\t<a href=\"#Character_Classes\">Character Classes<\/a>\n\t<\/li>\n\t<li>\n\t\t<a href=\"#Predefined_Character_Classes\">Predefined Character Classes<\/a>\n\t<\/li>\n\t<li>\n\t\t<a href=\"#Quantifiers\">Quantifiers<\/a>\n\t<\/li>\n\t<li>\n\t\t<a href=\"#References\">References<\/a>\n\t<\/li>\n<\/ol>\n<\/ol>\n<\/ol>\n<\/div>\n<div class='wptoc-end'>&nbsp;<\/div>\n<span id=\"Test_Harness\"><h3>Test Harness<\/h3><\/span>\n<pre lang=\"java\">\r\nimport java.io.Console;\r\nimport java.util.regex.Pattern;\r\nimport java.util.regex.Matcher;\r\n\r\npublic class RegexTestHarness {\r\n\r\n    public static void main(String[] args){\r\n        Console console = System.console();\r\n        if (console == null) {\r\n            System.err.println(\"No console.\");\r\n            System.exit(1);\r\n        }\r\n        while (true) {\r\n\r\n            Pattern pattern = \r\n            Pattern.compile(console.readLine(\"%nEnter your regex: \"));\r\n\r\n            Matcher matcher = \r\n            pattern.matcher(console.readLine(\"Enter input string to search: \"));\r\n\r\n            boolean found = false;\r\n            while (matcher.find()) {\r\n                console.format(\"I found the text\" +\r\n                    \" \\\"%s\\\" starting at \" +\r\n                    \"index %d and ending at index %d.%n\",\r\n                    matcher.group(),\r\n                    matcher.start(),\r\n                    matcher.end());\r\n                found = true;\r\n            }\r\n            if(!found){\r\n                console.format(\"No match found.%n\");\r\n            }\r\n        }\r\n    }\r\n}\r\n<\/pre>\n<span id=\"String_Literals\"><h3>String Literals<\/h3><\/span>\n<p>* Exact match:<\/p>\n<pre lang=\"bash\">\r\nEnter your regex: foo\r\nEnter input string to search: foo\r\nI found the text foo starting at index 0 and ending at index 3.\r\n<\/pre>\n<p>* Match multiple times:<\/p>\n<pre lang=\"bash\">\r\nEnter your regex: foo\r\nEnter input string to search: foofoofoo\r\nI found the text foo starting at index 0 and ending at index 3.\r\nI found the text foo starting at index 3 and ending at index 6.\r\nI found the text foo starting at index 6 and ending at index 9.\r\n<\/pre>\n<p>* Match metadata (in regex <em>cat.<\/em> dot means any character):<\/p>\n<pre lang=\"bash\">\r\nEnter your regex: cat.\r\nEnter input string to search: cats\r\nI found the text cats starting at index 0 and ending at index 4.\r\n<\/pre>\n<span id=\"Metacharaters\"><h3>Metacharaters<\/h3><\/span>\n<pre lang=\"bash\">\r\n<([{\\^-=$!|]})?*+.>\r\n<\/pre>\n<p>* Escape metacharaters<br \/>\n&#8211; precede with a backslash <em>\\<\/em><br \/>\n&#8211; enclose with <em>\\Q<\/em> and <em>\\E<\/em><\/p>\n<span id=\"Character_Classes\"><h3>Character Classes<\/h3><\/span>\n<p>* Character class is a set of characters enclosed with square brackets.<\/p>\n<pre lang=\"bash\">\r\n[abc]    a, b, or c (simple class)\r\n[^abc]    any character except a, b, or c (negation)\r\n[a-zA-Z]    a to z or A to Z, i.e. all alphabets, inclusive (range)\r\n[a-d[m-p]]    a to d, or m to p, same as [a-dm-p] (union)\r\n[a-z&&[def]]    d, e, or f (intersection)\r\n[a-z&&[^bc]]    a to z, except for b and c, same as [ad-z] (subtraction)\r\n<\/pre>\n<span id=\"Predefined_Character_Classes\"><h3>Predefined Character Classes<\/h3><\/span>\n<pre lang=\"bash\">\r\n.    any character\r\n\\d    a digit, same as [0-9]\r\n\\D    a non digit, same as [^0-9]\r\n\\s    a whitespace character, same as [ \\t\\n\\x0B\\f\\r]\r\n\\S    a non-whitespace character, same as [^\\s]\r\n\\w    a word character, same as [a-zA-Z_0-9]\r\n\\W    a non-word character, same as [^\\w]\r\n<\/pre>\n<span id=\"Quantifiers\"><h3>Quantifiers<\/h3><\/span>\n<p>* Specify the number of occurrences to match against.<\/p>\n<span id=\"References\"><h3>References<\/h3><\/span>\n<p>* Official Java Tutorial: <a href=\"http:\/\/docs.oracle.com\/javase\/tutorial\/essential\/regex\/intro.html\">Regular Expressions<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Test Harness import java.io.Console; import java.util.regex.Pattern; import java.util.regex.Matcher; public class RegexTestHarness { public static void main(String[] args){ Console console = System.console(); if (console == null) { System.err.println(&#8220;No console.&#8221;); System.exit(1); } while (true) { Pattern pattern = Pattern.compile(console.readLine(&#8220;%nEnter your regex: &#8220;)); &hellip; <a href=\"https:\/\/jianmingli.com\/wp\/?p=4638\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[133],"tags":[559,607],"class_list":["post-4638","post","type-post","status-publish","format-standard","hentry","category-regex","tag-java","tag-regex"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8cRUO-1cO","_links":{"self":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts\/4638","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4638"}],"version-history":[{"count":5,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts\/4638\/revisions"}],"predecessor-version":[{"id":7188,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts\/4638\/revisions\/7188"}],"wp:attachment":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4638"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4638"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4638"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}