Regex Special Characters in Java

Regex (Regular Expression) is a useful tool for manipulating, searching, and processing text strings. It simplifies and reduces the number of lines in a program.
We’ll look at how special characters are utilized in regex.
Special Characters in Java Regular Expressions
Regex is a type of textual syntax representing patterns for text matching. Regular expressions make use of special characters such as .
, +
, *
, ?
, ^
, $
, (
, )
, [
, ]
, {
, }
, |
, \
.
Characters in a regular expression (those in the string representing its pattern) are either metacharacters with a special meaning or regular characters with a literal meaning.
Metacharacter | Use | Example |
---|---|---|
^ |
start of the string or negation symbol | ^a matches a at the start of the string |
. |
matches any single character except the newline | a.[0-9] matches a string that has an a followed by a character and a digit |
[] |
Bracket expression that matches a single character contained within the brackets | [a-c] equals to either a or b or c , i.e., a|b|c also [abc] |
[^] |
matches a single character not contained in the brackets | [^abc] matches any character other than a , b , or c |
$ |
end of the line | ^abc$ matches a string that starts and ends with abc |
() |
grouping characters | (ab)\1 matches abab |
* |
Matches the preceding element or zero or more times | ab*c matches ac , abc , abbbc , etc. |
{m,n} |
Matches the preceding element at least m times and not more than n times | a{3,5} matches aaa , aaaa , aaaaa |
? |
Matches the preceding element zero or one time | ab?c matches ac , abc |
+ |
Matches the preceding element one or more times | ab+c matches abc , abbc , abbbc , etc., but not ac |
| |
the choice operator, it matches either the expression before or expression after the operator | |
ab|def matches either ab or def |
\ |
Escape or backslash | common escape sequences like \n or newline, \t for tab |
Example of Using Regex Special Characters in Java
In this example, we used the Pattern
and Matcher
classes from the java.util.regex
package. The Pattern
class represents a regular expression.
When we create an instance of the Pattern
class, we pass the regular expression as a string.
Here we have a regular expression regex_pattern
as a String. This pattern is used to check if a password meets the following criteria.
- At least one digit
[0-9]
must be included in the password. - At least one lowercase character
[a-z]
is required in the password. - At least one uppercase character
[A-Z]
is required in the password. - At least one special character, such as
! @ # & ()
, must be included in the password. - A password must be at least 8 characters long and no more than 24 characters long.
The compile()
method creates a pattern from the specified regular expression. We’ll use this pattern to make a Matcher
object later.
This pattern can be matched with any character sequence against the regular expression using the Matcher
object. The matcher
method creates a matcher that matches the given input against the pattern.
We match two strings to the pattern and print the output (a Boolean returned by the matches()
method).
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexTest {
public static void main(String [] args) {
String regex_pattern =
"^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[!@#&()–[{}]:;',?/*~$^+=<>]).{8,24}$";
Pattern pattern = Pattern.compile(regex_pattern);
String inputPassword1 = "Password1@@1990";
String inputPassword2 = "Pass190";
Matcher matcher1 = pattern.matcher(inputPassword1);
Matcher matcher2 = pattern.matcher(inputPassword2);
System.out.println("Password 1 matches pattern : "+matcher1.matches());
System.out.println("Password 2 matches pattern : "+matcher2.matches());
}
}
Output:
Password 1 matches pattern : true
Password 2 matches pattern : false
Rupam Saini is an android developer, who also works sometimes as a web developer., He likes to read books and write about various things.
LinkedInRelated Article - Java Regex
- Use Regex in the String.contains() Method in Java
- Use \s in Java
- String Matches Regex in Java
- Regex Whitespace in Java