If you are a new developer you mayh have heard of a Regular Expression, often shortened to regex or regexp and wondered what in the world they are and what they are useful for.

A Regular Expression is a concept in computer science that allows for matching complex patterns in text. If you have used search and replace in software you will have done things where you have replaced all instances of say Mr Smith with Mister Smith in a body of text, or just searched for a word.

But a regular expression is more powerful. It can let you find more complex patterns. For example you could look for all cases in a document where you have a set of square brackets with a number inside them and then replace the square brackets something else while leaving the number as is, but not affecting any other square brackets.

Most editors and IDEs have a feature for searching or replacing on regular expressions, as do most programming languages.

Lets look at some basic regular expressions…

Most characters will match themselves.

so the regex a will match the letter a, and the regex car will match a string with the letters car in it.

But what if you want to have letter that may or may not be there? You can signify that by adding a question mark to the end, so https? will match both http and https.

// EXPANED

Or you can look for a pattern like ‘(([0-9]+))’ which would look for a run of digits enclosed in two sets of ‘(())’, Note that here the backslashes say that we want to look for a literal parens and not use it as a special character.