Gluing regular expressions
1. Gluing regular expressions
In the previous lessons, you've learned how glue and glue collapse can be useful in various circumstances. You might have realized that so far, this had nothing to do with regular expressions. That's true, but in this lesson this is going to change.2. Collapsing with the pipe
Do you remember for example the pipe operator? It allowed us to write an "or condition" in our regular expression. You constructed a pattern that would match one of three words "Nemo", "Harmony" or "Dory". Imagine you have these three names stored somewhere in a variable. The vector could look something like this. You have learned how to collapse vectors into strings separated by a custom separator in the last exercise. Well, if we use the pipe operator as a separator, we can create the very same pattern from our vector "names". At first this might look more complicated, but I want to point out how useful this can be in certain situations. For example when you don't know the possible names in advance.3. A quick refresh
Let's very briefly go over the patterns that you learned in chapter one: You used character classes to match certain types of characters, like digits, words or spaces. You created custom patterns for alphabetical letters.4. A quick refresh
And you saw that with a plus sign, you can match one or more occurrences of something and with an asterisk you match any number of occurrences - including no occurrences.5. Break up complex patterns
Let's look at another way that we can use glue collapse to create a regular expression. Let's imagine we have a lot of text, but are interested in finding a name (Adam here) followed by two numbers, first the number of attempted logins and then of the successful logins, each separated by comma and space. Well sure, we could write the following pattern that would match this: One or more alphabetical letters, followed by comma and space, followed by one or more digits, and a gain a comma a space and one or more digits. Yikes! That looks pretty complicated. Maybe we know exactly what we are doing here, but what about colleagues that read our code? Or what about our future selves? Will we, in half a year from now, still understand what the pattern describes? Well with glue collapse we can construct the very same pattern also like this. As we can pass any vector, also one that has names, we can name the different parts of our pattern. The names won't appear in the pattern, but it is a very nice way to kind of self document the patterns that we write. We can write each part of the pattern onto its own line which breaks it up into many small, digestible pieces. By naming the elements of the vector we can make it even more clear what we are matching.6. Let's practice!
In the next chapter we will further improve these kinds of patterns and capture the different parts separately. But first, let's practice constructing regular expressions with glue collapse.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.