In this post, we will explore the utilization of regular expressions in Java. Regular expressions serve as an API for specifying string patterns in Java, enabling the search, manipulation, and editing of strings. They are commonly employed in defining string constraints, such as password validation. A regular expression is essentially a unique character sequence that provides a means to match or locate other strings or sets of strings through specialized syntax encapsulated within a pattern. This feature grants them the capability to search, edit, and manipulate text and data.

The package java.util.regex encompasses the functionalities related to regular expressions, consisting of three classes and one interface: Pattern, Matcher, PatternSyntaxException, and MatchResult Interface. The Pattern class is pivotal in crafting and compiling the regular expression pattern. It acts as the container where the regular expression is formulated. Subsequently, the Matcher class is instrumental in discovering matches between the input string and the specified pattern. It operates by repeatedly subjecting the input string to the regular expression pattern.

Pattern class

The Pattern class plays a crucial role in defining and validating patterns against input strings. The validation process is facilitated through the methods provided by the Pattern class. The static matcher methods are employed to match the pattern against the input string. Additionally, the compile method serves the purpose of validating the regular expression syntax against the specified regular expression string.

The pattern technique involves utilizing the matcher function to create a Matcher object, which is then employed to validate the input string. Furthermore, the split method utilizes the regular expression to split the input string, ultimately returning a string array.

package com.test;

import java.util.regex.Pattern;

public class JavaTest {

	public static void main(String[] args) {
		boolean flag = Pattern.matches("a*b", "ab");
		System.out.println(flag);
	}
}

Matcher class

The Matcher class, when provided with a pattern object and an input string, processes the input string using the specified regular expression and produces diverse results based on the methods invoked within the Matcher class. This class is proficient at locating occurrences of a regular expression in the given input, effectively matching the input string. Additionally, the Matcher can perform string splitting based on the provided regular expression, allowing segmentation of the input string according to the defined grouping in the regular expression. Moreover, the Matcher class facilitates iteration through the input string using the regular expression, with the assistance of methods like start and end to navigate within the input string.

package com.test;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class JavaTest {

	public static void main(String[] args) {
		Pattern pattern = Pattern.compile("a*b");
		Matcher m = pattern.matcher("abs");
		System.out.println(m.find());
	}
}

Find the matched String

The following example showcases the presentation of all matched strings within the given input, determined by the defined regular expression.

package com.test;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class JavaTest {

	public static void main(String[] args) {
		String str = "absaabmabsaaaaba";
		Pattern pattern = Pattern.compile("a*b");
		Matcher m = pattern.matcher(str);
		while (m.find()) {
			System.out.println(str.substring(m.start(), m.end()));
		}
	}
}

Split the String by regular expression delimiter

In the preceding example, the string is split by a regular expression delimiter, resulting in the inverse of the previous scenario. Instead of returning the matched string, the remaining characters are captured, and the matched string is excluded. This approach is often employed for parsing CSV files with delimiters like commas or spaces. The split method separates the input string based on the specified regular expression, storing the segments in a string array, which is then returned as the output.

package com.test;

import java.util.regex.Pattern;

public class JavaTest {

	public static void main(String[] args) {
		String str = "absaabmabsaaaaba";
		Pattern pattern = Pattern.compile("a*b");
		String s[] = pattern.split(str);
		for (String r:s) {
			System.out.println(r);
		}
	}
}

Capturing Groups

Capturing groups in regular expressions are logical sets of characters treated as a single unit. They are numbered from left to right based on the count of opening parentheses. Group 0 is a special group that represents the entire expression and is not included in the groupCount total. Groups are denoted by the use of left and right parentheses in the regular expression, encapsulating the pattern that matches the substring between them.

package com.test;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class JavaTest {

	public static void main(String[] args) {
		String str = "http://www.example.com";
		Pattern pattern = Pattern.compile("(http)://((w*)[.]([a-z]*)[.](com|net|org|in))");
		Matcher m = pattern.matcher(str);
		if(m.find()) {
			System.out.println(m.group(0));
			System.out.println(m.group(1));
			System.out.println(m.group(2));
			System.out.println(m.group(3));
			System.out.println(m.group(4));
			System.out.println(m.group(5));
		}
	}
}

Output

http://www.example.com
http
www.example.com
www
example
com

Leave a Reply