The Power Of Regular Expressions, Regex

The power of regular expressions (regex)

 
In this article, we are going to explore the world of the Regular Expression - what a Regular Expression or regex is and how we could take advantage of using it.
 

What is a Regular Expression?

 
Regular expressions are expressions that are made to match a specific pattern. As an example, there are regular expressions to verify the authenticity of emails, phone numbers, and websites.
 

Benefits of using Regular Expression

  • Wide range of usage possibility, you may create one regular expression to validate any kind of input;
  • Supported by almost any language, there are only a few programming languages which do not understand regular expressions;
  • Do more with less, keep your code cleaner;
  • Faster validations, instead of having many IF and ELSE operators you may validate only once with a regular expression.

Understanding some of the most used regular expression operators

 
1.  ? 
 
Matches the preceding character, zero or one time, and is case sensitive

Usage example
 
The Power Of Regular Expressions, Regex
  1. The first match has ac but not b;
  2. The second match has acb.
The Power Of Regular Expressions, Regex
  1. The first match has a42;
  2. The second match has a42. PS: only 1 a is matching. 
The Power Of Regular Expressions, Regex
  1. The first match has acb but not d; 
 2. *
 
Matches the preceding character, zero or more times, and is case sensitive

Usage example
 
The Power Of Regular Expressions, Regex
  1. The first match has ac but not b;
  2. The second match has acb.
The Power Of Regular Expressions, Regex
  1. The first match has a42;
  2. The second match has aaa42. PS.: 3 a are matching. 
  3. The third match has 42 but no a.
3. +
 
Matches the preceding character, one or more, and is case sensitive

Usage example
 
The Power Of Regular Expressions, Regex
  1. The first match has a42;
  2. The second match has aaa42. PS: a is matching 3 times.  
4. {N}
 
Matches the preceding character exactly N times 

Usage example
 
The Power Of Regular Expressions, Regex
  1. The first match has a42;
  2. The second match has a42. 
5. {N, } 
 
Matches the preceding character N or more times

Usage example
 
The Power Of Regular Expressions, Regex
  1. The first match has a42;
  2. The second match has aaa42. 
6. {N, M} 
 
Matches the preceding character between N and M times

Usage example
 
The Power Of Regular Expressions, Regex
  1. First match has aaa42;
  2. Second match has aa42.  
7.  \b \b  or  \B \b
 
\b \b Matches the exact word between the tags while \B \b does not match the exact word between the tags

Usage example
 
The Power Of Regular Expressions, Regex
  1. The first match has a42c;
The Power Of Regular Expressions, Regex
  1. The first match has acb.
The Power Of Regular Expressions, Regex
  1. The first match has acb. 
The Power Of Regular Expressions, Regex
  1. The first match has acb. 
8. ^
 
Matches the start position of a line

Usage example
 
The Power Of Regular Expressions, Regex
  1. The first match has abc at the beginning of the line. 
The Power Of Regular Expressions, Regex
  1. There are no matches, no lines starting with abd. 
9. $ 
 
Matches the end position of a line

Usage example
 
  1. The first match has jacbu ending the line.
The Power Of Regular Expressions, Regex
  1. There are no matches, no lines ending with abc. 
10. \b
 
Matches the end position of a string

Usage example
 
The Power Of Regular Expressions, Regex
  1. The first match has acb ending a string:
  2. The second match has acb ending a string.
11. \B
 
Matches a string that is not at the end position of a string

Usage example
 
The Power Of Regular Expressions, Regex
  1. The first match has acb not ending a string;
  2. The second match has acb not ending a string;
  3. The third match has acb not ending a string. 
12. X|Y  or  [XY]
 
Matches the X or Y character, is case sensitive

Usage example
 
The Power Of Regular Expressions, Regex
  1. The first match has ab, followed by a c;
  2. The second match has ac; 
  3. The third match has ac, followed by a b;
  4. The fourth match has ac, followed by bd;
  5. The fifth match has ac, between 2 and b;
  6. The sixth match has ac, among j, b and u;
  7. The seventh match has ac, followed by bd.
13. XY  or  (XY)
 
Matches the X and Y character, is case sensitive

Usage example
 
The Power Of Regular Expressions, Regex
  1. The first and second items match with acbd.
14. \d
 
Matches a digit character

Usage example
 
The Power Of Regular Expressions, Regex
  • All 10 matches are digits. 
15. \w 
 
Matches a word character

Usage example
 
The Power Of Regular Expressions, Regex
  • All 48 matches refer to the word characters. 
16. \s
 
Matches a space character

Usage example
 
The Power Of Regular Expressions, Regex
  • All 12 matches refer to the spaces between the words. 
17.  . 
 
Matches any character, except for line terminators

Usage example
 
The Power Of Regular Expressions, Regex
  • All 60 matches refer to every character, including spaces, in the line. 
Congratulations, you have improved your knowledge about Regular Expressions.
 
External References