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
- The first match has ac but not b;
- The second match has acb.
- The first match has a42;
- The second match has a42. PS: only 1 a is matching.
- The first match has acb but not d;
2. *
Matches the preceding character, zero or more times, and is case sensitive
- The first match has ac but not b;
- The second match has acb.
- The first match has a42;
- The second match has aaa42. PS.: 3 a are matching.
- The third match has 42 but no a.
3. +
Matches the preceding character, one or more, and is case sensitive
- The first match has a42;
- The second match has aaa42. PS: a is matching 3 times.
4. {N}
Matches the preceding character exactly N times
- The first match has a42;
- The second match has a42.
5. {N, }
Matches the preceding character N or more times
- The first match has a42;
- The second match has aaa42.
6. {N, M}
Matches the preceding character between N and M times
Usage example
- First match has aaa42;
- 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
- The first match has a42c;
8. ^
Matches the start position of a line
- The first match has abc at the beginning of the line.
- There are no matches, no lines starting with abd.
9. $
Matches the end position of a line
- The first match has jacbu ending the line.
- There are no matches, no lines ending with abc.
10. \b
Matches the end position of a string
- The first match has acb ending a string:
- The second match has acb ending a string.
11. \B
Matches a string that is not at the end position of a string
- The first match has acb not ending a string;
- The second match has acb not ending a string;
- 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 first match has ab, followed by a c;
- The second match has ac;
- The third match has ac, followed by a b;
- The fourth match has ac, followed by bd;
- The fifth match has ac, between 2 and b;
- The sixth match has ac, among j, b and u;
- The seventh match has ac, followed by bd.
13. XY or (XY)
Matches the X and Y character, is case sensitive
Usage example
- The first and second items match with acbd.
14. \d
Matches a digit character
Usage example
- All 10 matches are digits.
15. \w
Matches a word character
Usage example
- All 48 matches refer to the word characters.
16. \s
Matches a space character
Usage example
- All 12 matches refer to the spaces between the words.
17. .
Matches any character, except for line terminators
Usage example
- All 60 matches refer to every character, including spaces, in the line.
Congratulations, you have improved your knowledge about Regular Expressions.
External References