In this Java Regex Tutorial we want to talk about Java Regex Syntax, Java is popular programming language, and it is used for building different types of applications. and one of the features that Java has is support for regular expressions also we can call it regex. Regular expressions are powerful way to match, search and manipulate text. in this article we want to talk about the syntax of Java regex, including different characters and symbols that is used for creating patterns and matching the text.
Java Regex Syntax
We can say that regular expression is simply a pattern of characters that matches a specific sequence of text. in Java we can use Pattern class for creating a regular expression pattern. after that we can use this pattern to search, match or replace text.
This is a simple example of regular expression pattern in Java, this pattern matches the string hello exactly. you can use this pattern to search for instances of hello in a larger string like this:
1 |
Pattern pattern = Pattern.compile("hello"); |
This is another example, in here Matcher class represents a matcher for a particular pattern, and you can use it to search for matches in a string. in this case find() method returns true because the string hello is found in the larger string hello world.
1 2 |
Matcher matcher = pattern.matcher("hello world"); boolean found = matcher.find(); // returns true |
Characters and Symbols in Java Regex
. | dot Matches any single character except a line break. |
* | Matches zero or more occurrences of the preceding character or group. |
+ | Matches one or more occurrences of the preceding character or group. |
? | Matches zero or one occurrences of the preceding character or group. |
[] | Matches any one character from the set of characters inside the brackets. For example, [abc] matches either “a”, “b”, or “c”. |
^ | Matches the beginning of a line. |
$ | Matches the end of a line. |
| | Matches either the expression before or after the symbol. For example, a|b matches either “a” or “b”. |
() | Groups expressions together. |
These are a few examples of how these characters and symbols can be used in regular expressions.
1 2 3 4 5 6 7 8 9 10 11 |
// Matches any word that contains letter "a" Pattern pattern = Pattern.compile(".*a.*"); // Matches any string that starts with "hello" and ends with "world" Pattern pattern = Pattern.compile("^hello.*world$"); // Matches any string that contains either "apple" or "orange" Pattern pattern = Pattern.compile("apple|orange"); // Matches any string that contains either "apple" or "orange", but not both Pattern pattern = Pattern.compile("(apple|orange)(?!.*\\1)"); |
This is a complete Java code example that demonstrates how to use regular expressions to validate an email address, in this example we have defined a regular expression pattern EMAIL_REGEX that matches valid email addresses. after that we have defined a static method validateEmail that takes an email address as a parameter, compiles the pattern and matches it against the email address using Matcher object. this method returns true if the email address matches the pattern, and it indicates that it is a valid email address, otherwise it will say false, it means that email address is not valid.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
import java.util.regex.Matcher; import java.util.regex.Pattern; public class Main { private static final String EMAIL_REGEX = "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$"; public static boolean validateEmail(String email) { Pattern pattern = Pattern.compile(EMAIL_REGEX); Matcher matcher = pattern.matcher(email); return matcher.matches(); } public static void main(String[] args) { String email1 = "code.loop@codeloop.org"; String email2 = "code-loop@codeloop.org"; String email3 = "code_loop@codeloop.org"; String email4 = "admin@codeloop.org"; String email5 = "newsletter@codeloop"; // This should be invalid System.out.println(email1 + " is valid: " + validateEmail(email1)); System.out.println(email2 + " is valid: " + validateEmail(email2)); System.out.println(email3 + " is valid: " + validateEmail(email3)); System.out.println(email4 + " is valid: " + validateEmail(email4)); System.out.println(email5 + " is valid: " + validateEmail(email5)); // Should print false } } |
Run the code and you can see that last email address is not valid
Subscribe and Get Free Video Courses & Articles in your Email