Get startedGet started for free

Lookaround

1. Lookaround

In this final video, we will look into specific types of non-capturing groups. They help us look around an expression.

2. Looking around

Look-around will look for what is behind or ahead of a pattern. Imagine that we have the following string.

3. Looking around

We want to see what is surrounding a specific word. For example, we position ourselves in the word cat. So look-around will let us answer the following problem. At my current position, look ahead and search if sat is there. Or look behind and search if white is there.

4. Look-ahead

We'll start by exploring look-ahead. This non-capturing group checks whether the first part of the expression is followed or not by the lookahead expression. As a consequence, it will return the first part of the expression. In the previous example, we are looking for the word cat. The look ahead expression can be either positive or negative. For positive we use question mark equal. For negative question mark exclamation mark.

5. Positive look-ahead

Let's start with positive lookahead. Let's imagine that we have a string containing file names and the status of that file as shown in the code. We want to extract only those files that are followed by the word transferred. So we start building the regex by indicating any word character followed by dot txt.

6. Positive look-ahead

We now indicate we want the first part to be followed by the word transferred. We do so by writing question mark equal and then whitespace transferred all inside the parenthesis. With that specification, we get only the desired strings as shown in the output.

7. Negative look-ahead

Now, let's use negative lookahead in the same example.

8. Negative look-ahead

In this case, we will say that we want those matches that are NOT followed by the expression transferred. We use instead question mark exclamation mark inside parenthesis as seen in the code. Now, we get this other output.

9. Look-behind

The non-capturing group look-behind gets all matches that are preceded or not by a specific pattern. As a consequence, it will return the matches after the look expression. Let's use the same example, but now we are looking before the word cat. Look behind expression can also be either positive or negative. For positive we use question mark angle bracket equal. For negative question mark angle bracket exclamation mark.

10. Positive look-behind

Let's look at the following string. We want to find all matches of the names that are preceded by the word member. How do we construct our regex with positive look-behind? Let's examine the code. At the end of the regex, we'll indicate we want a sequence of word characters whitespace another sequence of word characters.

11. Positive look-behind

Pay attention to the code. The look-behind expression goes before that expression. We indicate question mark angle bracket equal followed by member, colon, and whitespace. All inside parentheses. In that way we get the two names that were preceded by the word member as shown in the output.

12. Negative look-behind

Now, we have this other string. We will use negative look-behind. We will find all matches of the word cat or dog that are not preceded by the word brown. In this code example, we use question mark angle bracket exclamation mark, followed by brown, whitespace. All inside the parenthesis. Then, we indicate our alternation group: cat or dog. Consequently, we get cat as an output. The cat or dog word that is not after the word brown.

13. Let's practice!

Now, it's your time to practice looking around!

Create Your Free Account

or

By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.