0% found this document useful (0 votes)
27 views2 pages

Regex

This document is a cheat sheet for Java Regular Expressions (Regex), detailing basic syntax, anchors, quantifiers, character classes, groups, and escaping. It includes common examples of regex usage for string validation, searching, and manipulation using the Pattern and Matcher classes. Additionally, it provides information on using regex with String methods like replaceAll and split, as well as flags for case sensitivity.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views2 pages

Regex

This document is a cheat sheet for Java Regular Expressions (Regex), detailing basic syntax, anchors, quantifiers, character classes, groups, and escaping. It includes common examples of regex usage for string validation, searching, and manipulation using the Pattern and Matcher classes. Additionally, it provides information on using regex with String methods like replaceAll and split, as well as flags for case sensitivity.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

Java Regular Expressions (Regex) Cheat Sheet

Note:
- Regex is used with the Pattern and Matcher classes in java.util.regex
- Commonly used for string searching, validation, parsing, etc.

--------------------------------------------------

Basic Syntax
. Matches any character except newline
\d Digit (0-9)
\D Non-digit
\w Word character (a-z, A-Z, 0-9, _)
\W Non-word character
\s Whitespace (space, tab, etc.)
\S Non-whitespace

--------------------------------------------------

Anchors
^ Start of string
$ End of string
\b Word boundary
\B Not a word boundary

--------------------------------------------------

Quantifiers
* 0 or more times
+ 1 or more times
? 0 or 1 time
{n} Exactly n times
{n,} n or more times
{n,m} Between n and m times

--------------------------------------------------

Character Classes
[abc] Matches 'a', 'b', or 'c'
[^abc] Not 'a', 'b', or 'c'
[a-z] Any lowercase letter
[A-Z] Any uppercase letter
[0-9] Any digit

--------------------------------------------------

Groups & Alternation


(ab) Group
a|b a or b
(?:...) Non-capturing group

--------------------------------------------------

Escaping
\\ Escape character in Java (double backslash)
\\. Literal dot
\\d Use two backslashes in Java strings

Example:
String regex = "\\d{3}-\\d{2}-\\d{4}"; // SSN format

--------------------------------------------------

Common Examples

// Check if string contains digits


str.matches(".*\\d+.*");

// Validate email
str.matches("^[\\w.-]+@[\\w.-]+\\.\\w+$");

// Check if string starts with "Hello"


str.matches("^Hello.*");

// Check if string is only numbers


str.matches("\\d+");

// Extract numbers from string


Pattern p = Pattern.compile("\\d+");
Matcher m = p.matcher("abc123def456");
while (m.find()) {
System.out.println(m.group()); // 123, 456
}

--------------------------------------------------

Pattern & Matcher Usage

import java.util.regex.*;

Pattern pattern = Pattern.compile("a*b");


Matcher matcher = pattern.matcher("aaab");

if (matcher.matches()) {
System.out.println("Match found!");
}

--------------------------------------------------

String.replaceAll() with Regex

String result = "abc123xyz".replaceAll("\\d", "*");


// Output: abc***xyz

--------------------------------------------------

String.split() with Regex

String[] parts = "one,two three".split("[,\\s]+");


// Splits on comma or whitespace

--------------------------------------------------

Flags (Pattern.CASE_INSENSITIVE, etc.)

Pattern pattern = Pattern.compile("java", Pattern.CASE_INSENSITIVE);


Matcher matcher = pattern.matcher("Java");
System.out.println(matcher.matches()); // true

You might also like