From the course: Learning Regular Expressions
What are regular expressions? - Regular Expressions Tutorial
From the course: Learning Regular Expressions
What are regular expressions?
- [Narrator] Let's begin by answering the question, what are regular expressions? Regular expressions are a tool for searching and matching parts of a text by describing the patterns that should be used to identify those parts. A regular expression is a set of symbols that describes a text pattern. The plural of the name regular expressions, refers to the larger formal language of the symbols that describe text patterns. Regular expressions are not a programming language. They're used for matching, searching or replacing text. Now, they may seem similar to a programming language because they are a formal language that have a defined set of rules that a computer understands, but there are no variables. They don't set values. They don't contain instructions, and they can't make decisions. But most programming languages make lots of use of regular expressions. They're an important and useful tool for working with text. Every programmer should learn how to use them. Often regular expressions are called regex for short. Sometimes you'll see it written with a P at the end, but that's not as common. You'll hear me say regex frequently because it's shorter and easier to say than regular expressions. Let's look at some of the ways that you might use regexes to work with text. You might test if a credit card number has the correct number of digits, or you might use them to test if an email address is in a valid format. You could search a document for the word color and look for it spelled both ways, C-O-L-O-R or C-O-L-O-U-R. You could also search a document for all occurrences of Bob, Bobby, or B period and replace it with the more formal Robert. Or we could count how many times the word training is immediately proceeded by the words computer, video or online. In each case, we would use regular expressions to write a description of the desired pattern using symbols. For a phone number, the pattern might be three digits, then a dash, then three more digits, then another dash, and finally four digits. We can use symbols to describe that pattern. Once we've defined our pattern, then the regex processor will use our description to look for matching results. The word matches is a keyword, and we're going to be using it a lot. A regular expression can be said to match text if it correctly describes the text. You can also flip that around and say that text matches a regular expression if it's correctly described by the expression. You can use it both ways. Regular expressions are patterns of symbols that match text.
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.