Wednesday, September 23, 2015

Demystifying RegEx with Practical Examples

A regular expression is a sequence of characters used for parsing and manipulating strings. They are often used to perform searches, replace substrings and validate string data. This article provides tips, tricks, resources and steps for going through intricate regular expressions.

regular expression

There are many books, articles, websites and the PHP official documentation that explain regular expressions, so instead of writing another explanation I’d prefer to go straight to more practical examples. You can find a useful cheat sheet at this link.

Along with a host of useful resources, there is also a conference video by Lea Verou at the bottom of this post - it’s a bit long, but it’s excellent in breaking down RegEx.

How to Build a Good Regex

Regular expressions are often used in the developer’s daily routine - log analysis, form submission validation, find and replace, and so on. That’s why every good developer should know how to use them, but what is the best practice to build a good regex?

1. Define a Scenario

Using natural language to define the problem will give you a better idea of the approach to use. The words could and must, used in a definition, are useful to describe mandatory constraints or assertions.

Below is an example:

  • The string must start with ‘h’ and finish with ‘o’ (e.g. hello, halo).
  • The string could be wrapped in parentheses.

2. Develop a Plan

After having a good definition of the problem, we can understand the kind of elements that are involved in our regular expression:

  • What are the types of characters allowed (word, digit, new line, range, …)?
  • How many times must a character appear (one or more, once, …)?
  • Are there some constraints to follow (optionals, lookahead/behind, if-then-else, …)?

Continue reading %Demystifying RegEx with Practical Examples%


by Nicola Pietroluongo via SitePoint

No comments:

Post a Comment