BreadcrumbHomeResourcesBlog Java Regular Expressions (Regex) Cheat Sheet March 8, 2017 Java Regular Expressions (Regex) Cheat Sheet Developer ProductivityJava Application DevelopmentIn this blog post, we explore Java regular expressions, including how they're used, best practices, and shortcuts to help you use them. We also provide a Java Regex cheat sheet PDF that gives you all sorts of Regex shortcuts on one page for future reference. Table of ContentsWhat Is a Java Regular Expression?Useful Regex Java Classes & MethodsJava Regex SyntaxCharacter Classes in Java Regular ExpressionsPredefined Character Classes in Java RegexJava Regex Boundary MatchersJava Regex Logical OperationsRegex Java QuantifiersRegular Expression Groups and BackreferencesRegular Expressions Pattern FlagsDownload the Regex Cheat Sheet PDFAdditional ResourcesTable of Contents1 - What Is a Java Regular Expression?2 - Useful Regex Java Classes & Methods3 - Java Regex Syntax4 - Character Classes in Java Regular Expressions5 - Predefined Character Classes in Java Regex6 - Java Regex Boundary Matchers7 - Java Regex Logical Operations8 - Regex Java Quantifiers9 - Regular Expression Groups and Backreferences10 - Regular Expressions Pattern Flags11 - Download the Regex Cheat Sheet PDF12 - Additional ResourcesBack to topWhat Is a Java Regular Expression?A Java regular expression, or Java Regex, is a sequence of characters that specifies a pattern which can be searched for in a text. A Regex defines a set of strings, usually united for a given purpose.Suppose you need a way to formalize and refer to all the strings that make up the format of an email address. Since there are a near infinite number of possible email addresses, it'd be hard to enumerate them all.However, as we know an email address has a specific structure, and we can encode that using the Regex syntax. A Java Regex processor translates a regular expression into an internal representation which can be executed and matched against the text being searched. It will tell you whether a string is in the set of strings defined by a pattern or find a substring that belongs in that set.Back to topUseful Regex Java Classes & MethodsMost languages have a regular expressions implementation either baked in or provided by a library. Java is no exception. Below are the classes you have to know in order to be effective using Regex Java.Regex Pattern MethodsPattern is a compiled representation of a regular expression in Java. Below is the list of the most frequently used methods in the Pattern class API for Regex Java.Java Regex Pattern MethodsRegex Pattern MethodDescriptionPattern compile(String regex)Compiles the given regular expression into a pattern.Pattern compile(String regex, int flags)Compiles the given regular expression into a pattern with the given flags.boolean matches(String regex)Returns whether or not this string matches the given regular expression.String[] split(CharSequence input)Splits the given input sequence around matches of this pattern.String quote(String s)Returns a literal pattern String for the specified String s.Predicate asPredicate()Creates a predicate which can be used to match a string.Regex Matcher Methods for Java Pattern MatchingA matcher is the engine that performs Java pattern matching operations on a character sequence by interpreting a Pattern. Below is the list of the most frequently used methods in the Matcher class API:Java Regex Matcher MethodsRegex Matcher MethodDescriptionboolean matches()Attempts to match the entire region against the pattern.boolean find()Attempts to find the next subsequence of the input that matches the pattern.int start()Returns the start index of the last match.By compiling a pattern and obtaining a matcher for it, you can match many texts for the pattern efficiently. So if you expect to process lots of texts, compile a matcher, cache it and use it repeatedly.Back to topJava Regex SyntaxLet's move on to the syntax for Java Regex. The Pattern.compile method takes a String, which is the RegEx that defines a set of matching strings. Naturally, it has to have a tricky syntax, otherwise a single string defining the pattern can only represent itself. A regular character in the Java Regex syntax matches that character in the text. If you'll create a Pattern with Pattern.compile("a") it will only match only the String "a". There is also an escape character, which is the backslash "\". It is used to distinguish when the pattern contains an instruction in the syntax or a character.Java Regex Escape ExampleLet’s look at an example as to why we need an escape character. Imagine "[" has a special meaning in the regular expression syntax (it has). How can you determine if "[" is a command to the matching engine or a pattern containing only the bracket? You cannot, so to specify the characters that are also the commands in the syntax you need to escape them. It means "\[" is a pattern for the string "[", and "[" is part of a command. What about trying to match a backslash? You need to escape it too, so be prepared to see something like "\\\\" in the regex code.Back to topCharacter Classes in Java Regular ExpressionsOn top of specifying the expressions that contain individual characters only, you can define the whole classes of characters. Think of them as sets, if a character in some text belongs to the character class, it is matched. Here is a table with the most used character classes in Java Regex.Java Regex Character ClassesCharacter ClassDescription[abc]simple, matches a or b, or c[\^abc]negation, matches everything except a, b, or c[a-c]range, matches a or b, or c[a-c[f-h]]union, matches a, b, c, f, g, h[a-c&&[b-c]]intersection, matches b or c[a-c&&[\^b-c]]subtraction, matches only aBack to topPredefined Character Classes in Java RegexFor your convenience, there are some useful classes defined already. For example, digits are a perfect example of a useful character class. For example a 5 digit number could be coded into a pattern as "[0-9][0-9][0-9][0-9][0-9]", but it's quite ugly. So there's a shorthand for that: "\d". Here are the other classes you need to know, starting with the regex for any character:Predefined Java Regex Character ClassesCharacter ClassDescription.Any character\dA digit: [0-9]\DA non-digit: [\^0-9]\sA whitespace character: [ \t\n\x0B\f\r]\SA non-whitespace character: [\^\s]\wA word character: [a-zA-Z_0-9]\WA non-word character: [\^\w]Note that the letter specifying a predefined character class is typically lowercase, the uppercase version tends to mean the negation of the class. Also, note that a dot "." is a character class, which contains all the characters. Particularly useful, but remember to escape it when you need to match the actual dot characterBack to topJava Regex Boundary MatchersNext, there's syntax to specify the position of the matched sequence in the original text you're searching. If you only need to filter out the strings that start with an email address or something, this is extremely useful.Java Regex Boundary MatchersBoundary Matcherstrong>Description^The beginning of a line$The end of a line\bA word boundary\BA non-word boundary\AThe beginning of the input\GThe end of the previous match\ZThe end of the input but for the final terminator, if any.\zThe end of the inputA noteworthy combination of the boundary matchers is the "^pattern$" which will only match the text if it is the full pattern.Back to topJava Regex Logical OperationsNow we’re getting into more advanced territory. If a pattern is more than a single character long, it will match a longer string too. In general "XY" in the Regex Java syntax matches X followed by Y. However, there's also an OR operation, denoted by the post "|". The "X|Y" Regex means it is either X or Y. This is a very powerful feature; you can combine the character classes or sequences of characters (include them in brackets).Back to topRegex Java QuantifiersOn top of everything, you can say how many times the sequence of characters can be repeated for the match. The Regex "1" only matches the input "1", but if we need to match a string of any length consisting of the character “1” you need to use one of the following quantifiers.Java Regex QuantifiersQuantifierDescription*Matches zero or more occurrences.+Matches one or more occurrences.?Matches zero or one occurrence.Back to topRegular Expression Groups and BackreferencesA group is a captured subsequence of characters which may be used later in the expression with a backreference. We've mentioned already that if you enclose a group of characters in parentheses, you can apply quantifiers or logical or to the whole group. What is even more awesome is that you can refer to the actual characters in the text matched by the group, later. Here's how you do it:Java Regex Groups and BackreferencesGroups and BackreferencesDescription(...)Matches zero or more occurrences.\NMatches one or more occurrences.(\d\d)Matches zero or one occurrence.(\d\d)/\1Two digits repeated twice, \1 - refers to the matched groupBack to topRegular Expressions Pattern FlagsRemember when we talked about the useful API for the regular expressions in Java, there was a method to compile a pattern that took the flags. These will control how the pattern behaves. Here are some flags that can be useful here and there.Java Regrx Pattern MethodsPattern FlagsDescriptionPattern.CASE_INSENSITIVEEnables case-insensitive matching.Pattern.COMMENTSWhitespace and comments starting with # are ignored until the end of a line.Pattern.MULTILINEOne expression can match multiple lines.Pattern.DOTALLThe expression "." matches any character, including a line terminator.Pattern.UNIX_LINESOnly the '\n' line terminator is recognized in the behavior of ., ^, and $.Back to topDownload the Regex Cheat Sheet PDFIn our Regular Expressions cheat sheet, we include essential Java classes and methods, Regex syntax, character classes, boundary matchers, logical operations, quantifiers, groups, backreferences, and pattern flags. You can download the Regex Cheat Sheet below. Download the Cheat Sheet Save Development Time With JRebelRegular expressions in Java make the coding process faster and less tedious for developers. JRebel provides an additional layer of efficiency by eliminating the costly downtime associated with code updates. See for yourself during your 14-day free trial. Try JRebel FreeBack to topAdditional ResourcesLast year, we explored some of the topics that are universally used in software development and have quite a library of useful Java cheat sheets to please your sight and remind you of the commands and options developers often forget:Java 8 Best PracticesJava 8 StreamsGit CommandsDocker CommandsJava CollectionsSQLJUnitSpring Framework AnnotationsJava GenericsJVM OptionsHow to Use Groovy RegEx For TestingBack to top