Regular Expressions

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.



  • .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.

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#

"form1" runat="server"> <asp:TextBox ID="txtName" runat="server"/> <asp:Button ID="btnSubmit" runat="server" Text="Submit" /> <asp:RegularExpressionValidator ID="regexpName" runat="server" ErrorMessage="This expression does not validate." ControlToValidate="txtName" ValidationExpression="^[a-zA-Z'.\s]{1,40}$" /> </form>

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:


Only letters, the Unicode way:


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;

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.

Follow me
I hope you would find above article informative and  interesting. In case you need any further information, please feel free to comment , I shall try to reply the comment at the earliest. If you like this article, please like my Facebook page and advise/suggest me for more topics of your interest. Happy Coding!