I have prepared how to write regex for each programming, including Java, PHP, and actual Sample Program.
I hope you will find them helpful.
1. How to handle email using regex
- Characters that can be used
3. Regex Notation in Each Programming Language
4. Example program to validate Email
- Execution results
- Explanation of Sample Program
1. How to handle email using regex
Regex can be used to check email and extract email from Text.
First, let us check what specifications the target email is based on.
What seems to be the latest specification for email is defined in RFC 5322 (Internet Message Format), "3.4.1. Addr-Spec Specification."
If you wish to implement this specification with regular expressions strictly, please read and understand the latest specification carefully before defining your regex.
Also, please do not use "." before "@" is not included in this specification.
The email format is described here according to the specification described in "dot-atom" in the RFC 5322 specification.
The email format specified in RFC 5322 is as follows. It is divided into two parts, local-part, and domain, with "@" in between.
basic format local-part @ domain
Characters that can be used
The characters that can be used in both "domain" and "local-part" are the following characters.
A "local-part" can be one or more of the following characters.
In addition, "." can be followed by one or more additional usable characters as a set.
However, "." alone cannot be added as the last character.
The same rules apply to "domain" as for "local-part."
2. Regex
Now let's see what happens to regex based on this email specification. The regex for an email would look like this. For now, it is the Java version.
This regex is complex, so I will explain how it is structured.
aText is the regex of the available characters.
The aText creates a regex dotAtom for strings containing dots. dotAtom starts with one or more aText characters.
Finally, the regex of an email is structured to be defined by dotAtom.
If you are not familiar with it, you can use it by copying and pasting it at first. You can also use more specific restrictions or more restrictive ones.
Since the characters used in email change little by little, it is advisable to use a somewhat more appropriate and looser expression.
3. Regex Notation in Each Programming Language
This comparison chart shows what happens when a programming language other than Java handles regex for a fixed email in Tokyo.
Language | Regex |
Java | |
Ruby | (Almost the same as Java, but the "/" needs to be replaced by "\/") |
PHP | (Same as Java, but sandwiched between "/". Also need to use "/" as "\/") |
C/C++ | (Almost same as Java, but "\w" needs to be replaced with "[:w:]") |
4. Example program to validate Email
This Java sample program uses regex to check whether an email matches the format. The email "abc.def@efg.com" is checked that email is correct and is detected as an invalid email if it is incorrect.
Execution Result
Explanation of Sample Program
[1] Call the method checkRightEmail (check for correct email).
[2] Call the method checkWrongEmail (check for wrong email).
[3] Define the method checkRightEmail.
[4] Display "correct email."
[5]-[7] The following email is passed as arguments to the method checkEmailByRegularExpression and validated.
[9] Define the method checkEmailes.
[10] Display "wrong email."
[20] Define the method checkEmailByRegularExpression.
[21] Define the variable "result."
[22] Assign the regex of the available characters to aText.
[23] Create a regex of a string containing dots by aText and assign it to dotAtom.
[24] Create a regex of email by dotAtom and assign it to the regex of the email.
[22]-[26] The method checkEmail is called to validate the regex for the email.
[27] Add the argument "email" to "fraud email:" and display it.
[40] Define the method checkEmail.
[41] Generate a Pattern instance pattern with the specified regex.
[42] Get a matcher from the pattern with the email specified as an argument.
[43][44][45] If the email matches the regex, the email is displayed, and the true is returned.
[46] Return false.
5. Summary
This article summarizes regex for email.
They are often used, but there is no need to memorize them. If you want to use it, please come to browse.
Java Book for Beginner
The most important selling points of Head First Java is its simplicity and super-effective real-life analogies that pertain to the Java programming concepts.
It is also the best book to learn java and start your learning journey with Java Development.
Head First Java covers almost all OOPS concepts and fascinatingly explains them.
Despite several readers claiming it to be a dated book, as it covers nothing beyond Java 5.0, Head First Java is still found on the shelves of numerous Java veterans.
Thus, it is a must-have book for every Java pursuer and developer.