What are Regular Expressions?

Lesson

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

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 more about this topic by checking out these references.


Other Lessons

Learn more by checking out these related lessons

Basic Regex Character Matching

lesson

View

Courses

This lesson is part of the following courses.

Learn Regular Expressions

course

View