Gmail is a free email service developed by Google. It is one of the most popular email services in the world. An email (short for “electronic mail”) is a message sent over a computer network, typically the internet, using a protocol called Simple Mail Transfer Protocol (SMTP). In this article let’s understand how we can create a regex for Gmail and how regex can be matched for Gmail.
Regex (short for regular expression) is a powerful tool used for searching and manipulating text. It is composed of a sequence of characters that define a search pattern. Regex can be used to find patterns in large amounts of text, validate user input, and manipulate strings. It is widely used in programming languages, text editors, and command line tools.
Structure of Gmail Address
- The first part should contain one or more characters that can appear before the
@
symbol in an email address. It includes all alphabetic characters (upper and lower case), digits, and some special characters. - The @ symbol is matched literally.
- The second part contains zero or one occurrence of a subdomain (which is itself made up of one or more alphabetic characters (upper and lower case), digits, and hyphens).
- The final part matches “gmail.com”
This regex will match most standard email addresses, but it may not match all possible email addresses due to the complexity of the email address specification. In particular, this regex does not support email addresses with quoted local parts (e.g., “[email protected]”), or email addresses with comments (e.g., “user@(comment)example.com”). This regex will match most standard email addresses, but it may not match all possible email addresses due to the complexity of the email address specification. In particular, this regex does not support email addresses with quoted local parts (e.g., “[email protected]”), or email addresses with comments (e.g., “user@(comment)example.com”).
Regex for checking if Gmail Address is valid
- The caret (^) symbol indicates the start of the string.
- The first part
[a-zA-Z0-9.!#$%&'*+/=?^_{|}~-]+
matches one or more characters that can appear before the@` symbol in an email address. It includes all alphabetic characters (upper and lower case), digits, and some special characters. - The @ symbol is matched literally.
- The second part
[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?
matches the main part of the email address, which can consist of one or more alphabetic characters (upper and lower case) and digits, followed by zero or one occurrence of a subdomain (which is itself made up of one or more alphabetic characters (upper and lower case), digits, and hyphens). - The final part contains gmail.com
- The dollar sign ($) indicates the end of the string.
Regular Expression-
/^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+(@gmail.com)$/gmi
Test string examples for the above regex-
Input String | Match Output |
---|---|
asd:[email protected] | does not match |
[email protected] | matches |
[email protected]:port | does not match |
[email protected] | matches |
This regex will match most standard email addresses, but it may not match all possible email addresses due to the complexity of the email address specification. In particular, this regex does not support email addresses with quoted local parts (e.g., “[email protected]”), or email addresses with comments (e.g., “user@(comment)example.com”).
Here is a detailed explanation of the above regex-
/^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+(@gmail.com)$/gmi
^ asserts position at start of a line
Match a single character present in the list below [a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]
+ matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)
a-z matches a single character in the range between a (index 97) and z (index 122) (case insensitive)
A-Z matches a single character in the range between A (index 65) and Z (index 90) (case insensitive)
0-9 matches a single character in the range between 0 (index 48) and 9 (index 57) (case insensitive)
.!#$%&'*+ matches a single character in the list .!#$%&'*+ (case insensitive)
\/ matches the character / with index 4710 (2F16 or 578) literally (case insensitive)
=?^_`{|}~- matches a single character in the list =?^_`{|}~- (case insensitive)
1st Capturing Group (@gmail.com)
@gmail matches the characters @gmail literally (case insensitive)
. matches any character (except for line terminators)
com matches the characters com literally (case insensitive)
$ asserts position at the end of a line
Global pattern flags
g modifier: global. All matches (don't return after first match)
m modifier: multi line. Causes ^ and $ to match the begin/end of each line (not only begin/end of string)
i modifier: insensitive. Case insensitive match (ignores case of [a-zA-Z])
It is important to note that using a regular expression to validate email addresses is not a foolproof method, as there are many subtleties and edge cases to consider. It is generally a better idea to use a library or service that is specifically designed for email validation.
Hope this article was useful to match gmail regex pattern. In conclusion, understanding and creating a regular expression (regex) for Gmail addresses can greatly aid in validating and managing email inputs. While the provided regex pattern covers the majority of standard Gmail addresses, it’s important to recognize its limitations, especially with regard to complex email address specifications. Utilizing regex for email validation is a helpful tool, but employing specialized email validation libraries or services is recommended for a more comprehensive approach to email verification.