In regular expressions, we can match any character using period "."
character. To match multiple characters or a given set of characters, we should use character classes.
1. Matching a Single Character Using Regex
By default, the '.'
dot character in a regular expression matches a single character without regard to what character it is. The matched character can be an alphabet, a number or, any special character.
To create more meaningful patterns, we can combine the dot character with other regular expression constructs.
Pattern | Description |
---|---|
. (Dot) | Matches only a single character. |
A.B | Matches only a single character at second place in a 3 character long string where the string starts with ‘A’ and ends with ‘B’. |
[abc] | Matches only a single character from a set of given characters. |
[aA] | Matches only a single character ‘a’, case-insensitive. |
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args)
{
Pattern.compile(".").matcher("a").matches(); //true
Pattern.compile(".").matcher("ab").matches(); //false
Pattern.compile("A.B").matcher("AIB").matches(); //true
Pattern.compile("A.B").matcher("ABI").matches(); //false
Pattern.compile("A[abc]B").matcher("AaB").matches(); //true
Pattern.compile("A[abc]B").matcher("AkB").matches(); //false
}
}
2. Matching Range of Characters
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’.
Pattern | Description |
---|---|
[a-f] | Matches only a single character in the range from ‘a’ to ‘f’. |
[a-z] | Matches only a single lowercase character in the range from ‘a’ to ‘z’. |
[A-Z] | Matches only a single uppercase character in the range from ‘A’ to ‘Z’. |
[a-zA-Z] | Matches only a single character in the range from ‘a’ to ‘z’, case-insensitive. |
[0-9] | Matches only a single number in the 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
}
}
3. Matching Multiple Characters
If we want to match a set of characters at any place then we need to use a wild card character ‘*
‘ (asterisk) which matches 0 or more characters.
Pattern | Description |
---|---|
.* | Matches any number of characters including special characters. |
[0-9]* | Matches any number of digits. |
[a-zA-Z]* | Matches any number of alphabets. |
[a-zA-Z0-9]* | Matches any number of alphanumeric characters. |
Pattern.compile(".*").matcher("abcd").matches(); //true
Pattern.compile("[a-zA-Z]*").matcher("abcd").matches(); //true
Pattern.compile("[0-9]*").matcher("01234").matches(); //true
Pattern.compile("[a-zA-Z0-9]*").matcher("a1b2c3").matches(); //true
Happy Learning !!