elibraryportal Logo

Regex in Java

In Java Regex, It stand for Regular Expression it provides an API that define pattern for searching or manipulating strings.

It is manily used to define on strings such as password and email validation. Java Regex API provides 1 interface and 3 classes in java.util.regex package.

java.util.regex package

The java.util.regex package provides following classes and interfaces for Regex.

  • Matcher class
  • Pattern class
  • PatternSyntaxException class
  • MatchResult interface

Matcher class

It implements the MatchResult interface.
No.MethodDescription
1boolean matches()test whether the regular expression matches the pattern.
2boolean find()finds the next expression that matches the pattern.
3boolean find(int start)finds the next expression that matches the pattern from the given start number.
4String group()returns the matched subsequence.
5int start()returns the starting index of the matched subsequence.
6int end()returns the ending index of the matched subsequence.
7int groupCount()returns the total number of the matched subsequence.

Pattern class

It is used to define a pattern for the regex engine.

No.MethodDescription
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.

Output

	true true true

Regular Expression . Example

The . (dot) represents a single character.

Ourput:-

Regex Example in Java

Regex Character classes

No.Character ClassDescription
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

Ourput:-

Regex Example in Java

Regex Quantifiers

The quantifiers specify the number of occurrences of a character.

RegexDescription
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

Ourput:-

Regex Example in Java

Regex Metacharacters

The regular expression metacharacters work as shortcodes.

RegexDescription
.Any character (may or may not match terminator)
\dAny digits, short of [0-9]
\DAny non-digit, short for [^0-9]
\sAny whitespace character, short for [\t\n\x0B\f\r]
\SAny non-whitespace character, short for [^\s]
\wAny word character, short for [a-zA-Z_0-9]
\WAny non-word character, short for [^\w]
\bA word boundary
\BA non word boundary

Regular Expression Metacharacters Example

Ourput:-

Regex Example in Java

Regular Expression Question 1

Ourput:-

Regex Example in Java

Regular Expression Question 2

Ourput:-

Regex Example in Java

Java Regex Finder Example

Output:

	Enter regex pattern: java
	Enter text: this is java, do you know java
	I found the text java starting at index 8 and ending at index 12
	I found the text java starting at index 26 and ending at index 30
	
Next TopicException Handling