From the course: Learning Regular Expressions
Unlock the full course today
Join today to access over 24,900 courses taught by industry experts.
Backreferences to optional expressions - Regular Expressions Tutorial
From the course: Learning Regular Expressions
Backreferences to optional expressions
- [Instructor] In this movie, we will learn to use backreferences to optional expressions. First, let's remind ourselves how optional elements work. In this expression, the a character is optional because it has that question mark after it. It matches both typical and atypical. If we put an optional character inside parenthesis the zero-width match will still be captured. It will capture the character if it exists. It will capture an empty string if it does not, so if we put parentheses around the optional a in the first example, it will match typical and capture an empty string. It still captures something. There's just no characters in it. The capture with zero-width. That means that the back reference will exist and will also be zero-width. In this example, the expression matches atypical and apolitical and also typical and political. The a was optional. When it was there, the backreference used it. When it…
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.