Regex – Match any character or set of characters

In regex, we can match any character using period "." character. To match only a given set of characters, we should use character classes.

1. Match any character using regex

'.' character will match any character without regard to what character it is. The matched character can be an alphabet, number of any special character.

By default, period/dot character only matches a single character. To create more meaningful patterns, we can combine it with other regular expression constructs.

PatternDescription
“.”Matches only a single character.
“A.B”Matches any character at second place in a 3 characters long string where string start with ‘A’ and ends with ‘B’.
“.*”Matches any number of characters.
import java.util.regex.Pattern;

public class Main {
	public static void main(String[] args) 
	{
		System.out.println(Pattern.compile(".").matcher("a").matches());	//true
		System.out.println(Pattern.compile(".").matcher("ab").matches());	//false

		System.out.println(Pattern.compile("A.B").matcher("AIB").matches());	//true
		System.out.println(Pattern.compile("A.B").matcher("ABI").matches());	//false
		
		System.out.println(Pattern.compile(".*").matcher("AIB").matches());		//true
	}
}

Program output.

true
false
true
false
true

2. Match fixed set of characters using regex

If we want to match a set of characters at any place, we need to use character classes. e.g. ‘[abc]’ will match a single character which can be either ‘a’, ‘b’ or ‘c’.

PatternDescription
“[abc]”Matches only a single character from set of given characters.
“[aA]”Matches character ‘a’, case-insensitive.
import java.util.regex.Pattern;

public class Main 
{
	public static void main(String[] args) 
	{
		System.out.println(Pattern.compile("[abc]").matcher("a").matches());	//true
		System.out.println(Pattern.compile("[abc]").matcher("ab").matches());	//false
		System.out.println(Pattern.compile("[abc]").matcher("d").matches());	//false
		
		System.out.println(Pattern.compile("[aA]").matcher("a").matches());		//true
		System.out.println(Pattern.compile("[aA]").matcher("A").matches());		//true
		System.out.println(Pattern.compile("[aA]").matcher("b").matches());		//false
	}
}

Program output.

true
false
false
true
true
false

3. Match range of characters using regex

If we want to match a range of characters at any place, we need to use character classes with a hyphen between the range. e.g. ‘[a-f]’ will match a single character which can be either of ‘a’, ‘b’, ‘c’, ‘d’, ‘e’ or ‘f’.

PatternDescription
“[a-f]”Matches only a single character in range from ‘a’ to ‘f’.
“[a-z]”Matches only a single lowercase character in range from ‘a’ to ‘z’.
“[A-Z]”Matches only a single uppercase character in range from ‘A’ to ‘Z’.
“[a-zA-Z]”Matches only a single character in range from ‘a’ to ‘z’, case-insensitive.
“[0-9]”Matches only a single number in range from ‘0’ to ‘9’.
import java.util.regex.Pattern;

public class Main 
{
	public static void main(String[] args) 
	{
		System.out.println(Pattern.compile("[a-f]").matcher("b").matches());	//true
		System.out.println(Pattern.compile("[a-f]").matcher("g").matches());	//false
		
		System.out.println(Pattern.compile("[a-zA-Z]").matcher("a").matches());	//true
		System.out.println(Pattern.compile("[a-zA-Z]").matcher("B").matches());	//true
		System.out.println(Pattern.compile("[a-zA-Z]").matcher("4").matches());	//false
		
		System.out.println(Pattern.compile("[0-9]").matcher("9").matches());	//true
		System.out.println(Pattern.compile("[0-9]").matcher("91").matches());	//false
	}
}

Program output.

true
false
true
true
false
true
true

Drop me your questions related to matching a single character, fixed set of characters or characters in a range using regular expressions in Java.

Happy Learning !!

Was this post helpful?

Join 7000+ Fellow Programmers

Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free.

1 thought on “Regex – Match any character or set of characters”

  1. I want to write a regex that matches any of the exact names (case insensitive) i provide. For example: the answer to a question could be any of the following “tom, peter, nancy, bill, novak”. So if the response is “Tom” or “tom” or “tOm” or “PETER” or “Peter”, the validation should come out as true. But if the response is “Tomas” or “peterson” or “billboard”, then the response is invalid.

    Reply

Leave a Comment

HowToDoInJava

A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python.