Hi All, Hope you are doing well. Let’s talk about Regular Expression today.
What is Regular Expressions or Regex
Regular Expressions or Regex or sometime called relational expressions are a special text pattern text string, commonly used for Search or validation. Most of the programming languages provide RegEx validations. As per wikipedia definition.
In computing, regular expressions provide a concise and flexible means for identifying strings of text of interest, such as particular characters, words, or patterns of characters. Regular expressions (abbreviated as regex or regexp, with plural forms regexes, regexps, or regexen) are written in a formal language that can be interpreted by a regular expression processor, a program that either serves as a parser generator or examines text and identifies parts that match the provided specification.
RegEx Patterns
Searching with regular expressions enables you to use single search to get multiple search results. For Example if you want to search “separate” and all of its common misspellings is easy with the regex s[ae]p[ae]r[ae]te.
Examples:
.at
matches any three-character string ending with “at”, including “hat”, “cat”, and “bat”.[hc]at
matches “hat” and “cat”.[^b]at
matches all strings matched by.at
except “bat”.[^hc]at
matches all strings matched by.at
other than “hat” and “cat”.^[hc]at
matches “hat” and “cat”, but only at the beginning of the string or line.[hc]at$
matches “hat” and “cat”, but only at the end of the string or line.\[.\]
matches any single character surrounded by “[” and “]” since the brackets are escaped, for example: “[a]” and “[b]”.s.*
matches s followed by zero or more characters, for example: “s” and “saw” and “seed”.
Let us explore an example of PHP Regular Expression to evaluate password strength
?php $password = "Fyfjk34sdfjfsjq7";
if (preg_match("/^.*(?=.{8,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).*$/", $password)) {
echo "Your passwords is strong."; } else { echo "Your password is weak."; } ?
Similarly we can use following Regular expression to search copyright years and replace them with desired one.
?php
echo preg_replace("/([Cc]opyright) 200(5|6|7|8)/", "$1 2009", "Copyright 2005");
?;
Regex is not limited to PHP, most languages provide RegEx processing, see below example in C#
Few RegEx Patterns examples
Valid Email : /^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/
Valid IP Address : /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/
Valid Website : /^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/
Only Letters
Only letters:
^[A-Za-z]+$
Only letters, the Unicode way:
^[[:alpha:]]+$
Identifying Internet Explorer Version using UserAgent
function ieVersion(ua){
var result = /MSIE (\d+)/.exec(ua); // extract major number of version
return result ? result[0] : null;
}
document.writeln(ieVersion(navigator.userAgent));
Regular Expressions saves lot of time and efforts, i would highly recommend to use RegEx in your codes.For more information on support of Regular expression in your programming language, kindly refer to documentation.
- Revolutionizing Justice: How AI is Reshaping India’s Legal System - August 20, 2023
- Exploring the Boundaries of Creativity: Artificial Intelligence and Generating Images, Pros and Cons - August 15, 2023
- The 10 Best Political TV Series of All Time - July 31, 2023