What are Regular Expressions?

Regular expressions are patterns specified with a defined syntax which are used to search through some text for specific sequences of characters. We write a regular expression and then use a compatible tool or programming language to search for that expression within some given text. When they are used correctly, regular expressions are a powerful tool. Regular expressions are used in systems administration, data processing, software engineering and much much more. Because the phrase 'regular expression' can be quite a mouthful, it is often contracted to 'regex' or sometimes 'regexp'.

How to Pronounce Regex

The 'correct' way to pronounce 'regex' is an ongoing debate which will remain unsolved (like the question of how to pronounce 'SQL'). It can be pronounced as either 'reg-eks' (where 'reg' rhymes with egg) or 'rej-eks' (where 'rej' rhymes with edge). Furthermore, some people use 'regexp' as either 'reg-eksp' or 'rej-eksp'. Terminology can vary depending on the implementation you are using (e.g. JavaScript vs Unix vs Perl) but most people will understand what you're talking about if you use any of them. If you want to avoid doubt, say 'regular expression' in full and specify the precise syntax you're using.

Why Learn Regular Expressions

We can use regular expressions in all kinds of situations, and the main reason to learn regex is that it can make your life easier. They give us a consistent way of searching across different systems, languages and domains so that we don't need to specify a new way of searching each time that we need to.

Example Uses of Regex

  • Searching a log file for lines which begin with 'URGENT'.
  • Validating data entered in web forms by users – e.g. checking that email addresses and URLs use a valid format.
  • You need to change all instances of the word 'red' in a document to 'blue' but don't want to replace it within words (e.g. 'kindred' shouldn't become 'kinblue').

There are a lot of times when you might need to conduct a search, and using regular expressions can save you countless lines of code. Uses can be broadly split into searching and replacing. Sometimes we want to check if some text has a match for our regex (e.g. searching a document for a string). Other times we want to match a pattern and then replace it with some new pattern (e.g. replacing 'red' with 'blue').

Regex for Beginners

Perhaps the most basic example of a regular expression is just matching literal text. For example, the regex 'Hello' could be used to search for 'Hello' in some text.

Example 1

Text to Search In

Hello there, how are you?

Regular Expression

Hello

Output

Hello there, how are you?

References

Learn, Build and Test RegEx

RegExr

Follow On Cyber Learning

lesson

Basic Regex Character Matching

Learn how to match standard character classes with regular expressions.

lesson

What are Regular Expressions?

Regular expressions are patterns specified with a defined syntax which are used to search text for specific sequences of characters with many practical applications.

Related Training Courses

course

Learn Regular Expressions

Regular expressions (regex) provide a flexible way of searching for searching using many different tools and programming languages.