Match any character or set of Characters - Java Regex

Regular expressions are a way to describe a set of strings based on common characteristics shared by each string in the set. They can be used to search, edit, or manipulate text and data. Java provides the java.util.regex package for pattern matching with regular expressions.

Regular expressions can be expressed as a short form as Regex.

Regular Expressions Represented in java.util.regex package

A Pattern object is a compiled representation of a regular expression. The Pattern class provides no public constructors. To create a pattern, you must first invoke one of its public static compile methods, which will then return a Pattern object. These methods accept a regular expression as the first argument; the first few lessons of this trail will teach you the required syntax.

A Matcher object is an engine that interprets the pattern and performs match operations against an input string. Like the Pattern class, Matcher defines no public constructors. You obtain a Matcher object by invoking the matcher method on a Pattern object.

A PatternSyntaxException object is an unchecked exception that indicates a syntax error in a regular expression pattern.

In this tutorial we will see Regex for – Match any character or set of characters.we can match any character using period "." character and character classes.

1). Matches only a single character - "."
2). Matches any character at second place in a 3 characters long string where the string starts with 'A' and ends with 'B' - "X.Y"
3). Matches any number of characters- ".*"

1). Matches only a single character

The Character will match any character. The matched character can be any character like the alphabet, a number of any special characters.

import java.util.regex.Pattern; public class MatchesSingleCharacter { public static void main(String[] args) { System.out.println(Pattern.compile(".").matcher("X").matches());//true System.out.println(Pattern.compile(".").matcher("XY").matches());//false System.out.println(Pattern.compile(".").matcher("xyz").matches());//false System.out.println(Pattern.compile(".").matcher("$").matches());//true System.out.println(Pattern.compile("X.X").matcher("XYX").matches());//true System.out.println(Pattern.compile("X.X").matcher("XYZ").matches());//false System.out.println(Pattern.compile(".*").matcher("XYYZ").matches());//true } } output: true false false true true false true

2). Match a fixed set of characters

If we want to match a fixed set of characters in a given place, we have to use the character classes. If we have [xyz], then the characters can be matched as x or y or z.

import java.util.regex.Pattern; public class MatchesFixedSetCharacters { public static void main(String[] args) { System.out.println(Pattern.compile("[XY]").matcher("X").matches());//true System.out.println(Pattern.compile("[XYZ]").matcher("X").matches());//true System.out.println(Pattern.compile("[xyz]").matcher("xy").matches());//false System.out.println(Pattern.compile("[xyz]").matcher("w").matches());//false System.out.println(Pattern.compile("[xX]").matcher("X").matches());//true System.out.println(Pattern.compile("[Xx]").matcher("x").matches());//true System.out.println(Pattern.compile("[zZ]").matcher("w").matches());//false } } output: true true false false true true false

3). Match range of characters

If we want to match the characters in between the given range, we need to use the character classes with a hyphen '-' character.

If we have given range as [u-z] then the character will be matched of any given character 'u','v','w', 'x','y','z' etc.

1). [p-w] - Matches only a single character in range from p to w.

2). [a-z] - Matches only a single character in range a to z.

3). [A-Z] - Matches only a single character in range A to Z.

4). [a-zA-Z] - Matches only a single character in range from a to z or A TO Z .ie case insensitive.

5). [0-9] - Matches only a single number within the range 0 t0 9.

import java.util.regex.Pattern; public class Main { public static void main(String[] args) { System.out.println(Pattern.compile("[p-z]").matcher("q").matches()); //true System.out.println(Pattern.compile("[a-u]").matcher("v").matches()); //false System.out.println(Pattern.compile("[a-zA-Z]").matcher("q").matches()); //true System.out.println(Pattern.compile("[a-zA-Z]").matcher("$").matches()); //false System.out.println(Pattern.compile("[a-zA-Z]").matcher("Z").matches()); //true System.out.println(Pattern.compile("[a-zA-Z]").matcher("11").matches()); //false System.out.println(Pattern.compile("[0-9]").matcher("7").matches()); //true System.out.println(Pattern.compile("[0-9]").matcher("55").matches()); //false } } output: true false true false true false true false

Post/Questions related to  Match any character or set of Characters - Java Regex

How to check special characters in java using Regex?
How to make regex case insensitive in java?
How to extract a substring from a string in java using Regex?
What does mean in regex Java?
What is a regular expression in Java?
How to escape special characters in java Regex?
Java regex to allow only alphanumeric characters
Credit Card Number Validation using Regex
Currency Symbols Validation using Regex
date validation Validation using Regex
email validation Validation using Regex
Password Validation Validation using Regex
Greek Extended or Greek Script Validation using Regex
Validate ISBN Validation using Regex
check Min/Max Length of Input Text Validation using Regex
limit the number of lines in text Validation using Regex
limit the number of words in input Validation using Regex 
Validate SSN (Social Security Numbers) Validation using Regex
Trademark Symbol Validation using Regex
international phone numbers Validation using Regex
North American Phone Numbers Validation using Regex

In this article, we have seen Match any character or set of Characters - Java Regex with Examples. All source code in the article can be found in the GitHub repository.