Introducing Regular Expressions

Regular Expressions (or Regex, or regexp) simplest definition would be: A sequence of characters defining a search pattern.


What does that mean exactly?


How many times have you seen a sequence of numbers written on a piece of paper and you immediately knew this is a phone number? And you were easily able to distinguish to which country it belongs, to which area in that country and even if it belongs to a mobile number or to a land line.


A lot of information acquired by one look to a number: simply by noticing the pattern!

This pattern and many other patterns are stored somewhere in the back of our minds, allowing us to validate any information we receive.


So for example:

  1. We know that: Lebanon phone numbers have the country code of +961 followed by exactly 8 digits.

  2. We ask a friend for their phone number and they provide us with : +961 123ABC

  • Our mind will compare the number with the pattern and instantly notify us there is an ERROR!

This is how the human mind works, And this is what Regular Expressions are for computers.


If an application, a website, or a software is requiring information about a phone number, we want the system to validate the format of the user's input. Therefore, we define the "Pattern" to which the computer can compare the input to.


Patterns can be:

  • As simple as: Restricting input to numbers only, or text only.

  • A bit more complex as : validating the format of email addresses and URLs.

  • More complex as custom pattern to specific codes.


Playing with RegEx:

Below is a list in which you can play with RegEx:

The link below have predefined patterns set in the top box, try entering any text under "TEST STRING" and check the result on the right section of the window.

  1. Validate only Arabic Input

  2. Only Numbers

  3. Only numbers of 9 digits starting with 961


Enjoy RegExing,

For a RegEx library and a simple RegEx generator, check the second post in the series: Regular Expression Generator




#programming #regex #tech #coding #kobo #regularexpression #validation #technology #geek

© 2017 by Rayan Al Zahab

This site was designed with the
.com
website builder. Create your website today.
Start Now