{"id":27161,"date":"2020-12-17T19:52:04","date_gmt":"2020-12-18T03:52:04","guid":{"rendered":"https:\/\/careerkarma.com\/blog\/?p=27161"},"modified":"2022-07-20T08:57:52","modified_gmt":"2022-07-20T15:57:52","slug":"java-regex","status":"publish","type":"post","link":"https:\/\/careerkarma.com\/blog\/java-regex\/","title":{"rendered":"Java Regex: An Introduction"},"content":{"rendered":"\n<p>There may come a time in your application development journey where you need to find patterns in strings. Some methods can take time to execute, while others save you time.<br><\/p>\n\n\n\n<p>One time-saving technique called regex \u2014 regular expressions \u2014 can be used to find or validate string patterns. This article takes a look at what regular expressions are, how they might be used in Java, and resources to help you with writing Java-flavored regex.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What are Regular Expressions?<\/h2>\n\n\n\n<p>At a very high level, regular expressions are just pattern matchers \u2014 characters that match a pattern. The actual metacharacters used to build a regular expression are fairly language agnostic for those languages that support using it. This means that the pattern in Java is most likely the same in JavaScript or Ruby.<br><\/p>\n\n\n\n<p>The methods in Java are fairly similar to those found in other languages as well. It\u2019s mainly the syntax that\u2019s different.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">What are Regular Expressions Used for?<\/h3>\n\n\n\n<p>A Regular Expression is useful when you need to search and replace a pattern in a string, and when you need to validate a form. Depending on the circumstances, you can test your regex pattern in a number of different ways.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Search and Replace<\/h4>\n\n\n\n<p>The first use case for using regular expressions would be if you want to search for a particular pattern and then replace it with something else.&nbsp;<br><\/p>\n\n\n\n<p>Let\u2019s say we have a customer database that has some email address for each customer:<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Sample Customer Database<\/h4>\n\n\n\n<table class=\"wp-block-table course-info-table\"><tbody><tr><td><strong>id<\/strong><\/td><td><strong>first_name<\/strong><\/td><td><strong>last_name<\/strong><\/td><td><strong>email<\/strong><\/td><\/tr><tr><td>1<\/td><td>Doris<\/td><td>Symcox<\/td><td>dsymcox0@elpais<\/td><\/tr><tr><td>2<\/td><td>Linnell<\/td><td>Friar<\/td><td>lfriar1@pinterest.com<\/td><\/tr><tr><td>3<\/td><td>Paulie<\/td><td>Mathes<\/td><td>pmathes2hud.gov<\/td><\/tr><tr><td>4<\/td><td>Davina<\/td><td>Boam<\/td><td>dboam3@people.com.cn<\/td><\/tr><tr><td>5<\/td><td>Hulda<\/td><td>Coneybeare<\/td><td>hconeybeare4@taobao.com<\/td><\/tr><tr><td>6<\/td><td>Jaymee<\/td><td>Barnes<\/td><td>jbarnes5@vistaprint.com<\/td><\/tr><tr><td>7<\/td><td>Wendy<\/td><td>Fley<\/td><td>wfley6@unesco..com<\/td><\/tr><tr><td>8<\/td><td>Torr<\/td><td>Rustich<\/td><td>trustich7@java.com<\/td><\/tr><tr><td>9<\/td><td>Norene<\/td><td>Redwing<\/td><td>nredwing8@umn.edu<\/td><\/tr><tr><td>10<\/td><td>Philbert<\/td><td>Merveille<\/td><td>pmerveille9@jimdo.com<\/td><\/tr><\/tbody><\/table>\n\n\n\n<p><\/p>\n\n\n\n<p>If you take a quick look at the database, you can see there are some typos in the email field. Now imagine having 1,000 of those fields! It would be too timely to check over each record by hand.&nbsp;<br><\/p>\n\n\n\n<p>If we are unsure as to whether or not all of the addresses are valid email addresses, we can use regex methods to make sure it has the correct format. If it does not, we can replace it with something else \u2014 either a null value or something of your choosing to indicate that the email is incorrect.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Validation<\/h4>\n\n\n\n<p>The other way to use regular expressions is to validate something. When we validate, we want to make sure it follows the correct format. This is an optimal time to make sure a user is giving you the proper format for their input fields.<br><\/p>\n\n\n\n<p>Take, for instance, when a user inputs a phone number into a form. You can use regex to write a function that makes certain that the input from the user is in the format we want. When working with databases, it\u2019s important to have the same format for all the fields. It makes working with the data much easier.<br><\/p>\n\n\n\n<p>For validators, you can either raise an error to the user that the input needs to be entered in a certain format or you can write a function that will take the user\u2019s input and save it in the format you\u2019d like. This is fairly easy to do if you use regular expressions.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Types of RegEx Pattern Matchers<\/h3>\n\n\n\n<p>There are several different types of matchers that will assist you with writing your regular expressions, which include: literal characters, metacharacters, and quantifiers.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Literal Characters<\/h4>\n\n\n\n<p>The most simple example of a regex is a literal string. The string \u201chello\u201d can be an example of a regex, as can this whole paragraph.<br><\/p>\n\n\n\n<p>Examples:&nbsp;<br><\/p>\n\n\n\n<p><code>\/hello\/<\/code> \u21d2 collection of five distinct characters.<br><\/p>\n\n\n\n<p>When a regex pattern is applied here, it looks for each of these characters in succession. \u201chello\u201d, \u201chelloing\u201d, or \u201chelloed\u201d passes, but \u201cHello\u201d, \u201chelo\u201d, or \u201cHeLlo\u201d would not.&nbsp;&nbsp;<br><\/p>\n\n\n\n<p><code>\/The most simple example of a regex is a literal string. The string \u2018hello\u2019 can be an example of a regex, as can this whole&nbsp;paragraph\\.\/<\/code> \u21d2 collection of characters that make up a paragraph can also be a regex.&nbsp;<br><\/p>\n\n\n\n<p>The same applies to this second example. The pattern looks for exactly that arrangement of characters in that exact same way when it\u2019s tested against a string.<br><\/p>\n\n\n\n<p>When a test is failed, the terminal will throw an error when it\u2019s executed.&nbsp;<br><\/p>\n\n\n\n<p>Take another look at the second example. Do you notice the forward slash (\\) next to the period (.)? This is what it means to <em>escape<\/em> a character. For the regex engine to overlook that and actually see it as a period, we have to escape it with a forward slash. The period, or dot, we will learn is a defined pattern matcher in regex.<br><\/p>\n\n\n\n<p>Here are some other characters that need to be escaped if you want the literal character instead of the translated meaning that the regex engine compiles it to.&nbsp;<br><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Asterisk *<\/li><li>Backslash \/<\/li><li>Plus +<\/li><li>Caret ^<\/li><li>Dollar Sign $<\/li><li>Dot\/Period .<\/li><li>Pipe |<\/li><li>Question Mark ?<\/li><li>Parentheses &#8211; both types ()<\/li><li>Curly Braces &#8211; both types {}<\/li><\/ul>\n\n\n\n<p>Literal characters return exactly that \u2014 the literal collection of characters you are looking for. If you need to look for special characters in addition to alphanumeric characters, be sure to escape the character so that the regex pattern goes looking for the wrong thing.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Common Matchers&nbsp;<\/h4>\n\n\n\n<p>The purpose of a matcher is to match multiple letters in a pattern. This collection of pattern matching symbols is fairly consistent among the programming languages that use regex.&nbsp;<br><\/p>\n\n\n\n<table class=\"wp-block-table course-info-table\"><tbody><tr><td><strong>Matcher<\/strong><\/td><td><strong>Description<\/strong><\/td><td><strong>Example<\/strong><\/td><\/tr><tr><td><strong>.<\/strong><\/td><td>Matches any character<\/td><td>\/n.w\/ would match now, naw, or new, etc. Any character passes the test<\/td><\/tr><tr><td>^regex<\/td><td>Looks for pattern at beginning of the line<\/td><td>\/^hello\/ would match hello in a line that started with that pattern<\/td><\/tr><tr><td>regex$<\/td><td>Looks for pattern at end of the line<\/td><td>\/world$\/ would match world in a line that ended with that pattern<\/td><\/tr><tr><td>[abc]<\/td><td>Matches a, b, or c&nbsp;<\/td><td>\/[misp]\/ would match any string that has those characters in it. For example, it could match all the individual letters in mississippi, and miss, but only some of the letters in marsh, and missouri<\/td><\/tr><tr><td>[abc][xyz]<\/td><td>Matches a, b, or c followed by x, y, or z<\/td><td>\/[Mm].s\/ would match any string that starts with M or m, followed by any character, followed by an ss.Mass, miss, moss, would all match<\/td><\/tr><tr><td>[^abc]<\/td><td><strong>Not<\/strong> a, b, or c<\/td><td>\/[^rstlne]\/ would match any character that is not r, s, t, l, n, or e<\/td><\/tr><tr><td>[a-zA-Z0-9]<\/td><td>Matches any character within the range<\/td><td>\/[a-n]\/ would match any character between a and n.and, end, blind, can, all have characters that entirely match here<\/td><\/tr><tr><td>A|B<\/td><td>A <strong>or<\/strong> B<\/td><td>\/^M|m.\/ would match any word two characters in length that matches either an M or an m at the beginning of a line<\/td><\/tr><tr><td>CAT<\/td><td>Matches C, followed by A, followed by T<\/td><td>\/hello world\/ would match hello world exactly<\/td><\/tr><\/tbody><\/table>\n\n\n\n<h4 class=\"wp-block-heading\"><\/h4>\n\n\n\n<h4 class=\"wp-block-heading\">Metacharacters<\/h4>\n\n\n\n<p>Regular expressions also use metacharacters to describe a pattern. Metacharacters have some sort of meaning behind them and will describe the shape of the pattern.<br><\/p>\n\n\n\n<table class=\"wp-block-table course-info-table\"><tbody><tr><td><strong>Metacharacter<\/strong><\/td><td><strong>Description<\/strong><\/td><td><strong>Example<\/strong><\/td><\/tr><tr><td>\\d<\/td><td>Matches any digit<\/td><td>\/\\d\/ would match 1, 2, or 3, etc. Shorthand for [0-9]<\/td><\/tr><tr><td>\\D<\/td><td>Matches any non-digit character<\/td><td>\/\\D\/ would match A, B, g, etc.. Shorthand for [^0-9]&nbsp;<\/td><\/tr><tr><td>\\s<\/td><td>Matches any whitespace character<\/td><td>\/\\s\/ would match new lines, tabs, spaces, etc. Shorthand for&nbsp;[\\t\\n\\x0b\\r\\f]<\/td><\/tr><tr><td>\\S<\/td><td>Matches any non-whitespace character&nbsp;<\/td><td>\/\\S\/ shorthand for&nbsp;[^\\t\\n\\x0b\\r\\f]<\/td><\/tr><tr><td>\\w<\/td><td>Matches any word character<\/td><td>A word character, short for [a-zA-Z_0-9]<\/td><\/tr><tr><td>\\W<\/td><td>Matches any non-word character<\/td><td>\/[\\W]\/ would match any special characters. Shorthand for [^\\w]<\/td><\/tr><\/tbody><\/table>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>Note:<\/strong> Capital letter metacharacters (\\W, \\D, etc) usually correspond to the opposite of what the lowercase letter metacharacters do (\\w, \\d, etc).&nbsp;<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Quantifiers<\/h4>\n\n\n\n<table class=\"wp-block-table course-info-table\"><tbody><tr><td><strong>Quantifier<\/strong><\/td><td><strong>Description<\/strong><\/td><td><strong>Example<\/strong><\/td><\/tr><tr><td>+<\/td><td>One of more of preceding character<\/td><td>\/\\d+\/ would match two or more digits<\/td><\/tr><tr><td>*<\/td><td>Zero or more of preceding character<\/td><td>\/.*\/ would match any character 0 or more times&nbsp;<\/td><\/tr><tr><td>?<\/td><td>Zero or one of the preceding character<\/td><td>\/a?.*\/ would match a, any, hello, world&nbsp;<br><\/td><\/tr><tr><td>{number}<\/td><td>Matches preceding character exactly <em>number<\/em> of times&nbsp;<\/td><td>\/\\d{3}\/ matches exactly three digits [0-9]<\/td><\/tr><tr><td>{num1,num2}<\/td><td>Matches preceding character in a range of nums<\/td><td>\/\\d{3,5}\/ matches 3 to 5 digits that are [0-9]<\/td><\/tr><\/tbody><\/table>\n\n\n\n<p><\/p>\n\n\n\n<p>Use the quantifiers, metacharacters, and other matchers as the building blocks for your regular expressions. The syntax mentioned here is synonymous across multiple languages. However, there are some things that are used, for instance, in Java, that would not be transferable to JavaScript.<br><\/p>\n\n\n\n<p>Let\u2019s learn a little more about how regular expressions work in Java.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How to use Regex in Java<\/h2>\n\n\n\n<p>When using regular expressions in Java, use the java.util.regex API. The actual regular expression pattern itself relies mostly on the syntax mentioned in the previous section. The actual creation of an instance of a regular expression and the actual match operation is reliant upon the three classes and one interface that are in the java.util.regex package.<br><\/p>\n\n\n\n<p>These classes and interface are:&nbsp;<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li><code>Pattern.<\/code> A compiled representation of a regular expression. The Pattern class contains a compile method that will take the pattern you create and compile it to a regular expression. This regular expression can be used elsewhere.&nbsp;<br><\/li><li><code>Matcher.<\/code> A class object that takes a Pattern instance and performs matching operations on it against an input string.&nbsp;<br><\/li><li><code>PatternSyntaxException.<\/code> An error class object that is thrown when a syntax error occurs in a regular expression.&nbsp;<br><\/li><li><code>MatchResult.<\/code> An interface that represents the result of a match operation. It has methods to help queries here can be seen but not modified.<\/li><\/ol>\n\n\n\n<p>Here is an example of this API in action (adapted from Oracle\u2019s documentation):&nbsp;<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>\/\/  \tCopyright (c) 1995, 2008, Oracle and\/or its affiliates. \n\/\/  \t*All rights reserved.\n \n \nimport java.io.Console;\nimport java.util.regex.Pattern;\nimport java.util.regex.Matcher;\nimport java.util.regex.MatchResult;\n \npublic class Main {\n public static void main(String[] args) {\n   Console console = System.console();\n   Pattern pattern = Pattern.compile(&quot;hEllO wORld&quot;, Pattern.CASE_INSENSITIVE);\n   Matcher matcher = pattern.matcher(&quot;hello world&quot;);\n   if(console == null) {\n     System.err.println(&quot;No console&quot;);\n     System.exit()\n   }\n \n   boolean found = false;\n   while(matcher.find()) {\n     MatchResult matchresult = matcher.toMatchResult();\n     console.format(&quot;%s%nRegex: %s%n&quot;, matchresult.group(), pattern.pattern());\n     found = true;\n   }\n   if(!found) {\n     console.format(&quot;No expression matches found%nRegex: %s&quot;, pattern.pattern());\n   }\n }\n}<\/pre><\/div>\n\n\n\n<p>This code snippet, adapted from the tutorial and documentation found on Oracle\u2019s website, illustrates use of the MatchResult interface along with the Pattern and Matcher classes.&nbsp;<br><\/p>\n\n\n\n<p>The Pattern object is the actual compiled regex itself, seen here with the regex pattern as the first argument. The second argument is a flag that indicates whether the method will treat upper and lowercase characters as the same character. <a href=\"https:\/\/docs.oracle.com\/javase\/tutorial\/essential\/regex\/pattern.html\" target=\"_blank\" rel=\"noopener\" rel=\"nofollow\">Flags<\/a> are optional but can be helpful if you have a particular need for one.<br><\/p>\n\n\n\n<p>The compiled regex pattern from the Pattern instance is used in the Pattern object\u2019s matcher method to create a Matcher object. This allows the object to have access to all of the methods in the Matcher class. One of these methods is the <code>find()<\/code> method.&nbsp;<br><\/p>\n\n\n\n<p>The <code>find()<\/code> method is a Boolean method that basically tells us whether or not the regex pattern can be found in the string you passed.<br><\/p>\n\n\n\n<p>In our code, we assign the found value to a MatchResult instance. The MatchResult is an immutable value that can only be queried and not changed. The <code>group()<\/code> method queries the portion of the string that matched the regex pattern that was applied. If you happen to need to know the indexes of the beginning and end of the string the pattern was matched to, you can grab it with <code>start()<\/code> and <code>end()<\/code>.<br><\/p>\n\n\n\n<p>This code can be used to test regex in your IDE. Test it out with other regex and strings to learn how it works.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Java Regex Resources<\/h2>\n\n\n\n<p>The documentation that surrounds the use of regex in Java requires some navigation. A lot of the top queries on the subject is older documentation that may not be necessarily up-to-date. Take a look at these resources to supplement the documentation that is on Oracle\u2019s website.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><a href=\"https:\/\/repl.it\/@careerkarma\/Regex#Main.java\" target=\"_blank\" rel=\"noopener\" rel=\"nofollow\">Java Regex Tester<\/a><\/h3>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh3.googleusercontent.com\/kqBCjdHb7E2TcdgQxBJjtjMas7OlMOSs90NXCRksEGbww4xmiQ8tv1XcPvuquDRMclCIxl_4SElBdEQWYiah6hLYPsxtrKOrNY1SZnkjmTkfnLI3jmW4E8KqQ50scbPbh4-TBs9M\" alt=\"Career Karma\u2019s repl.it with a Java Regex Tester\"\/><\/figure>\n\n\n\n<p>Use the Java Regex Tester that was adapted from Oracle\u2019s <a href=\"https:\/\/docs.oracle.com\/javase\/tutorial\/displayCode.html?code=https:\/\/docs.oracle.com\/javase\/tutorial\/essential\/regex\/examples\/RegexTestHarness.java\" target=\"_blank\" rel=\"noopener\" rel=\"nofollow\">RegexTestHarness<\/a>. Career Karma\u2019s version updates the code from where it was when Oracle\u2019s version was written and posted to its Regex tutorial. Replace the arguments in lines 41 and 42 with your regex and your string to test your regex pattern. Add flags as needed to your compile function.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><a href=\"https:\/\/www.regexplanet.com\/advanced\/java\/index.html\" target=\"_blank\" rel=\"noopener\" rel=\"nofollow\">Regex Planet<\/a><\/h3>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh5.googleusercontent.com\/5nF-3j8B02VRaedvcLo1JOmwCI__zWUmGFdQZzX8L4veJu1DFjCWne724qEztkmdT7KBF2oEI03Tabrb6vDAvlFzfRbWfnqYRBnMajKecdXOChpwSJNhI4U_wS5D3cwBS_H9eOCD\" alt=\"\"\/><\/figure>\n\n\n\n<p>Regex Planet gives us the ability to test regular expressions in Java. All you have to do is give it the regular expression, select the flags you want to enable, and then test it against multiple inputs that you give it.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><a href=\"https:\/\/www.jrebel.com\/blog\/java-regular-expressions-cheat-sheet\" target=\"_blank\" rel=\"noopener\" rel=\"nofollow\">Java Regular Expressions Cheatsheet<\/a><\/h3>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh4.googleusercontent.com\/ocfP1uye-52DFBo8TIIHP2suAXMvSXnbQ25-6iGr8cVzeKGA5d9Z1J8d-PJ5Ri2eDSKJpMWBgpqnIWZHx3nEmSv3ETCMhVeuSE-rN54VP8M6oh1F-GZSq5ad1l-HP8Yk7bNwjJNT\" alt=\"\"\/><\/figure>\n\n\n\n<p>This is a useful at-a-glance cheatsheet that compiles methods and classes and everything Java regex related onto one single page website. It is super helpful when you need to create your own regex and use it in business logic.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>In this article we\u2019ve covered what regular expressions are and how regular expressions are tested in Java. In short, regular expressions are just ways to match patterns in our code that help us to validate or search and replace values.<br><\/p>\n\n\n\n<p>Java\u2019s regular expression syntax is no more or no less difficult than it is for other languages. The adjustments mainly come from using Java\u2019s regex classes to use regular expressions in your logic.&nbsp;<br><\/p>\n\n\n\n<p>The best thing to do is to really just play with Java\u2019s flavor of regular expressions for a while. Once you see the patterns and realize what the characters stand for, regular expressions can become less overwhelming and more useful to use.<\/p>\n","protected":false},"excerpt":{"rendered":"There may come a time in your application development journey where you need to find patterns in strings. Some methods can take time to execute, while others save you time. One time-saving technique called regex \u2014 regular expressions \u2014 can be used to find or validate string patterns. This article takes a look at what&hellip;","protected":false},"author":77,"featured_media":27162,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[17289],"tags":[],"class_list":{"0":"post-27161","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-java"},"acf":{"post_sub_title":"","sprint_id":"","query_class":"Java","school_sft":"","parent_sft":"","school_privacy_policy":"","has_review":null,"is_sponser_post":"","is_guest_post":""},"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v27.4 (Yoast SEO v27.4) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Java Regex: A Complete Guide | Career Karma<\/title>\n<meta name=\"description\" content=\"In this tutorial from Career Karma, learn how to use regular expressions (regex) in Java. We&#039;ll take a look at the Pattern and Matcher classes as well as the MatchResult interface.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/careerkarma.com\/blog\/java-regex\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Java Regex: An Introduction\" \/>\n<meta property=\"og:description\" content=\"In this tutorial from Career Karma, learn how to use regular expressions (regex) in Java. We&#039;ll take a look at the Pattern and Matcher classes as well as the MatchResult interface.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/careerkarma.com\/blog\/java-regex\/\" \/>\n<meta property=\"og:site_name\" content=\"Career Karma\" \/>\n<meta property=\"article:publisher\" content=\"http:\/\/facebook.com\/careerkarmaapp\" \/>\n<meta property=\"article:published_time\" content=\"2020-12-18T03:52:04+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-07-20T15:57:52+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/12\/jessica-lewis-1tsaxPdyTLk-unsplash.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1020\" \/>\n\t<meta property=\"og:image:height\" content=\"680\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Christina Kopecky\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@career_karma\" \/>\n<meta name=\"twitter:site\" content=\"@career_karma\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Christina Kopecky\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"11 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/java-regex\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/java-regex\\\/\"},\"author\":{\"name\":\"Christina Kopecky\",\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/#\\\/schema\\\/person\\\/ae0cdc4a5d198690d78482646894074e\"},\"headline\":\"Java Regex: An Introduction\",\"datePublished\":\"2020-12-18T03:52:04+00:00\",\"dateModified\":\"2022-07-20T15:57:52+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/java-regex\\\/\"},\"wordCount\":2169,\"commentCount\":0,\"image\":{\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/java-regex\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/12\\\/jessica-lewis-1tsaxPdyTLk-unsplash.jpg\",\"articleSection\":[\"Java\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/careerkarma.com\\\/blog\\\/java-regex\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/java-regex\\\/\",\"url\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/java-regex\\\/\",\"name\":\"Java Regex: A Complete Guide | Career Karma\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/java-regex\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/java-regex\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/12\\\/jessica-lewis-1tsaxPdyTLk-unsplash.jpg\",\"datePublished\":\"2020-12-18T03:52:04+00:00\",\"dateModified\":\"2022-07-20T15:57:52+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/#\\\/schema\\\/person\\\/ae0cdc4a5d198690d78482646894074e\"},\"description\":\"In this tutorial from Career Karma, learn how to use regular expressions (regex) in Java. We'll take a look at the Pattern and Matcher classes as well as the MatchResult interface.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/java-regex\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/careerkarma.com\\\/blog\\\/java-regex\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/java-regex\\\/#primaryimage\",\"url\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/12\\\/jessica-lewis-1tsaxPdyTLk-unsplash.jpg\",\"contentUrl\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/12\\\/jessica-lewis-1tsaxPdyTLk-unsplash.jpg\",\"width\":1020,\"height\":680,\"caption\":\"Java (coffee) and coffee beans on birch table\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/java-regex\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Blog\",\"item\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java\",\"item\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/java\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Java Regex: An Introduction\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/\",\"name\":\"Career Karma\",\"description\":\"Latest Coding Bootcamp News &amp; Career Hacks from Industry Insiders\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/#\\\/schema\\\/person\\\/ae0cdc4a5d198690d78482646894074e\",\"name\":\"Christina Kopecky\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/06\\\/image-3-150x150.jpg\",\"url\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/06\\\/image-3-150x150.jpg\",\"contentUrl\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/06\\\/image-3-150x150.jpg\",\"caption\":\"Christina Kopecky\"},\"description\":\"Christina is an experienced technical writer, covering topics as diverse as Java, SQL, Python, and web development. She earned her Master of Music in flute performance from the University of Kansas and a bachelor's degree in music with minors in French and mass communication from Southeast Missouri State. Prior to joining the Career Karma team in June 2020, Christina was a teaching assistant, team lead, and section lead at Lambda School, where she led student groups, performed code and project reviews, and debugged problems for students. Christina's technical content is featured frequently in publications like Codecademy, Repl.it, and Educative.\",\"sameAs\":[\"http:\\\/\\\/www.linkedin.com\\\/in\\\/cmvnk\"],\"url\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/author\\\/christina-kopecky\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Java Regex: A Complete Guide | Career Karma","description":"In this tutorial from Career Karma, learn how to use regular expressions (regex) in Java. We'll take a look at the Pattern and Matcher classes as well as the MatchResult interface.","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:\/\/careerkarma.com\/blog\/java-regex\/","og_locale":"en_US","og_type":"article","og_title":"Java Regex: An Introduction","og_description":"In this tutorial from Career Karma, learn how to use regular expressions (regex) in Java. We'll take a look at the Pattern and Matcher classes as well as the MatchResult interface.","og_url":"https:\/\/careerkarma.com\/blog\/java-regex\/","og_site_name":"Career Karma","article_publisher":"http:\/\/facebook.com\/careerkarmaapp","article_published_time":"2020-12-18T03:52:04+00:00","article_modified_time":"2022-07-20T15:57:52+00:00","og_image":[{"width":1020,"height":680,"url":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/12\/jessica-lewis-1tsaxPdyTLk-unsplash.jpg","type":"image\/jpeg"}],"author":"Christina Kopecky","twitter_card":"summary_large_image","twitter_creator":"@career_karma","twitter_site":"@career_karma","twitter_misc":{"Written by":"Christina Kopecky","Est. reading time":"11 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/careerkarma.com\/blog\/java-regex\/#article","isPartOf":{"@id":"https:\/\/careerkarma.com\/blog\/java-regex\/"},"author":{"name":"Christina Kopecky","@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/ae0cdc4a5d198690d78482646894074e"},"headline":"Java Regex: An Introduction","datePublished":"2020-12-18T03:52:04+00:00","dateModified":"2022-07-20T15:57:52+00:00","mainEntityOfPage":{"@id":"https:\/\/careerkarma.com\/blog\/java-regex\/"},"wordCount":2169,"commentCount":0,"image":{"@id":"https:\/\/careerkarma.com\/blog\/java-regex\/#primaryimage"},"thumbnailUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/12\/jessica-lewis-1tsaxPdyTLk-unsplash.jpg","articleSection":["Java"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/careerkarma.com\/blog\/java-regex\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/careerkarma.com\/blog\/java-regex\/","url":"https:\/\/careerkarma.com\/blog\/java-regex\/","name":"Java Regex: A Complete Guide | Career Karma","isPartOf":{"@id":"https:\/\/careerkarma.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/careerkarma.com\/blog\/java-regex\/#primaryimage"},"image":{"@id":"https:\/\/careerkarma.com\/blog\/java-regex\/#primaryimage"},"thumbnailUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/12\/jessica-lewis-1tsaxPdyTLk-unsplash.jpg","datePublished":"2020-12-18T03:52:04+00:00","dateModified":"2022-07-20T15:57:52+00:00","author":{"@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/ae0cdc4a5d198690d78482646894074e"},"description":"In this tutorial from Career Karma, learn how to use regular expressions (regex) in Java. We'll take a look at the Pattern and Matcher classes as well as the MatchResult interface.","breadcrumb":{"@id":"https:\/\/careerkarma.com\/blog\/java-regex\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/careerkarma.com\/blog\/java-regex\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/careerkarma.com\/blog\/java-regex\/#primaryimage","url":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/12\/jessica-lewis-1tsaxPdyTLk-unsplash.jpg","contentUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/12\/jessica-lewis-1tsaxPdyTLk-unsplash.jpg","width":1020,"height":680,"caption":"Java (coffee) and coffee beans on birch table"},{"@type":"BreadcrumbList","@id":"https:\/\/careerkarma.com\/blog\/java-regex\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog","item":"https:\/\/careerkarma.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Java","item":"https:\/\/careerkarma.com\/blog\/java\/"},{"@type":"ListItem","position":3,"name":"Java Regex: An Introduction"}]},{"@type":"WebSite","@id":"https:\/\/careerkarma.com\/blog\/#website","url":"https:\/\/careerkarma.com\/blog\/","name":"Career Karma","description":"Latest Coding Bootcamp News &amp; Career Hacks from Industry Insiders","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/careerkarma.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/ae0cdc4a5d198690d78482646894074e","name":"Christina Kopecky","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/06\/image-3-150x150.jpg","url":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/06\/image-3-150x150.jpg","contentUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/06\/image-3-150x150.jpg","caption":"Christina Kopecky"},"description":"Christina is an experienced technical writer, covering topics as diverse as Java, SQL, Python, and web development. She earned her Master of Music in flute performance from the University of Kansas and a bachelor's degree in music with minors in French and mass communication from Southeast Missouri State. Prior to joining the Career Karma team in June 2020, Christina was a teaching assistant, team lead, and section lead at Lambda School, where she led student groups, performed code and project reviews, and debugged problems for students. Christina's technical content is featured frequently in publications like Codecademy, Repl.it, and Educative.","sameAs":["http:\/\/www.linkedin.com\/in\/cmvnk"],"url":"https:\/\/careerkarma.com\/blog\/author\/christina-kopecky\/"}]}},"_links":{"self":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/posts\/27161","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/users\/77"}],"replies":[{"embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/comments?post=27161"}],"version-history":[{"count":0,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/posts\/27161\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/media\/27162"}],"wp:attachment":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/media?parent=27161"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/categories?post=27161"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/tags?post=27161"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}