Rex eats regular expressions for breakfast. And so can you! This regex tutorial, one of the most detailed on the web, takes you all the way to mastery.
- Java Regex Cheat Sheet Examples
- Java Regex Cheat Sheet Excel
- Reg Expression Cheat Sheet
- Java Design Pattern Cheat Sheet
.test takes the regex, applies it to a string, and returns a boolean.replace replaces a substring with another substring and returns the modified string.match extracts the actual matches you found in the return array. In 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 Java RegEx Cheat Sheet, below. Download the Cheat Sheet. Regex Cheat Sheet A regular expression, or just regex, is used to match patterns within the given string. Bellow is the cheat sheet of string matching I find useful.
This page explains what makes this site special among all other regex sites, but first let's answer a burning question:
What is the meaning of life?
That's easy. As per the regex humor page, it's simply
^(?=(?!(.)1)([^DO:105-93+30])(?-1)(?<!d(?<=(?![5-90-3])d))).[^WHY?]$
Now for the other burning question…
What is a Regex?
First, a regex is a text string. For instance, foo is a regex. So is [A-Z]+:d+.Those text strings describe patterns to find text or positions within a body of text. For instance, the regex foo matches the string foo, the regex [A-Z]+:d+ matches string fragments like F:1 and GO:30, and the regex (?<=[a-z])(?=[A-Z]) matches the position in the string CamelCase where we shift from a lower-case letter to an upper-case letter.
Typically, these patterns (which can be beautifully intricate and precise) are used for four main tasks: to find text within a larger body of text; to validate that a string conforms to a desired format; to replace text (or insert text at matched positions, which is the same process); and to split strings.
For instance, the CamelCase pattern from the last paragraph can be used to split MyLovelyValentine into its three component words. And you could use the regex _d+_ to find digits within underscores (as in _12_) and to replace the underscores with double dashes, yielding --12--, something you could not do with a conventional search-and-replace (details for that technique are in the recipe about replacing one delimiter with another).
Java Regex Cheat Sheet Examples
Who does this work of finding, replacing, splitting? A regex engine. For instance, you can find regex engines in text editors such as Notepad++ and EditPad Pro. You also find regex engines ready to roar in most programming languages—such as C#, Python, Perl, PHP, Java, JavaScript and Ruby.
Let's compress the definition from the earlier paragraphs:
Is a Regex the same as a Regular Expression?
Mostly yes, with a little bit of no. At this stage, this is a semantic question—it depends on what one means by regular expression. That topic and other juicy details are discussed on the page about Regex vs. Regular Expressions.
About this Site
Before we dive in—and only if you have time—I'd like to introduce this site and what makes it special.I love regular expressions. They are a small computer language of their own.
When I was a young dinosaur, I didn't take the time to properly learn the syntax, largely because I really didn't feel like learning another language. Who needs regex, I thought, when your programming language has functions that let you dig into strings from the left, the middle and the right?
What's more, the raw syntax you usually see in code that contains regexes used to intimidate me. Who wants to deal with a language that looks like this?
(?s)/*(?:(?!*/)[*$ _/+-])*(.*?)[*$ _/+-]*?*/
No doubt about it, even with comments and breathing room, there is something raw and experimental about writing a regex pattern.
Besides, how well your pattern performs doesn't only depend on applying correct syntax. There are several ways of doing things, and various regex engines may optimize some of these ways behind your back.
With regex, you are stepping down to a fairly low level, within earshot of the machine room. I like that. And I've been liking it all the more since learning about tools and safeguards to keep me from falling into the boiler.
A (hopefully) Different Presentation of Regex
RexEgg tries to present regular expressions a bit differently, in the hope that these different angles help many people become more grounded in their knowledge of regex. If you are looking for a drawn-out primer, this is not the place, as I don't see the need to pollute our beautiful world wide web with another explanation of how to match 'foo' in 'foo bar'. But if you take your time to read the carefully-built tables on the quick-launch page then perhaps the page about (? … ) syntax, you will experience what may be the most accelerated regex introduction around.
What Will you Find on this Site
✽ A step-by-step explanation of simple and advanced regular expressions crafted for various contexts (such as text matching, file renaming, search-and-replace).

✽ A presentation of the many contexts where you may run into regular expressions (from Apache to your html editor and file manager), complete with examples.
✽ A reference about (? … )—to reduce confusion by bringing all the pieces of syntax that start with an opening parenthesis and a question mark into a single place.
✽ A discussion of Conditional Regexes, a topic about which there is little information.
✽ A discussion of Recursive Regexes, a topic about which there is very little information.
✽ Pages dedicated to regex in C#, Python, PHP and other languages.
✽ Plenty of tips & tricks.
✽ Sections about
Java Regex Cheat Sheet Excel
regex tools and regex books.
✽ And much more!
I wish you lots of fun on your journey with regular expressions.
Smiles,
Rex
and The Best Regex Trick Ever!!!
Quick-Start: Regex reference table
Reg Expression Cheat Sheet
The Java Regex or Regular Expression is an API to define a pattern for searching or manipulating strings.
It is widely used to define the constraint on strings such as password and email validation. After learning Java regex tutorial, you will be able to test your regular expressions by the Java Regex Tester Tool.
Java Regex API provides 1 interface and 3 classes in java.util.regex package.
java.util.regex package
The Matcher and Pattern classes provide the facility of Java regular expression. The java.util.regex package provides following classes and interfaces for regular expressions.
- MatchResult interface
- Matcher class
- Pattern class
- PatternSyntaxException class
Matcher class
It implements the MatchResult interface. It is a regex engine which is used to perform match operations on a character sequence.
No. | Method | Description |
---|---|---|
1 | boolean matches() | test whether the regular expression matches the pattern. |
2 | boolean find() | finds the next expression that matches the pattern. |
3 | boolean find(int start) | finds the next expression that matches the pattern from the given start number. |
4 | String group() | returns the matched subsequence. |
5 | int start() | returns the starting index of the matched subsequence. |
6 | int end() | returns the ending index of the matched subsequence. |
7 | int groupCount() | returns the total number of the matched subsequence. |
Pattern class
It is the compiled version of a regular expression. It is used to define a pattern for the regex engine.
No. | Method | Description |
---|---|---|
1 | static Pattern compile(String regex) | compiles the given regex and returns the instance of the Pattern. |
2 | Matcher matcher(CharSequence input) | creates a matcher that matches the given input with the pattern. |
3 | static boolean matches(String regex, CharSequence input) | It works as the combination of compile and matcher methods. It compiles the regular expression and matches the given input with the pattern. |
4 | String[] split(CharSequence input) | splits the given input string around matches of given pattern. |
5 | String pattern() | returns the regex pattern. |
Example of Java Regular Expressions
There are three ways to write the regex example in Java.
Test it NowOutput
Regular Expression . Example
The . (dot) represents a single character.
Java Design Pattern Cheat Sheet
Test it NowRegex Character classes
No. | Character Class | Description |
---|---|---|
1 | [abc] | a, b, or c (simple class) |
2 | [^abc] | Any character except a, b, or c (negation) |
3 | [a-zA-Z] | a through z or A through Z, inclusive (range) |
4 | [a-d[m-p]] | a through d, or m through p: [a-dm-p] (union) |
5 | [a-z&&[def]] | d, e, or f (intersection) |
6 | [a-z&&[^bc]] | a through z, except for b and c: [ad-z] (subtraction) |
7 | [a-z&&[^m-p]] | a through z, and not m through p: [a-lq-z](subtraction) |
Regular Expression Character classes Example
Test it NowRegex Quantifiers
The quantifiers specify the number of occurrences of a character.
Regex | Description |
---|---|
X? | X occurs once or not at all |
X+ | X occurs once or more times |
X* | X occurs zero or more times |
X{n} | X occurs n times only |
X{n,} | X occurs n or more times |
X{y,z} | X occurs at least y times but less than z times |
Regular Expression Character classes and Quantifiers Example
Test it NowRegex Metacharacters
The regular expression metacharacters work as shortcodes.
Regex | Description |
---|---|
. | Any character (may or may not match terminator) |
d | Any digits, short of [0-9] |
D | Any non-digit, short for [^0-9] |
s | Any whitespace character, short for [tnx0Bfr] |
S | Any non-whitespace character, short for [^s] |
w | Any word character, short for [a-zA-Z_0-9] |
W | Any non-word character, short for [^w] |
b | A word boundary |
B | A non word boundary |
Regular Expression Metacharacters Example
Test it NowRegular Expression Question 1
Test it NowRegular Expression Question 2
Test it Now
Java Regex Finder Example
Output:
