 Regular expressions or regex is a useful technique for finding patterns in text strings. In this video series, the series, I'll explain how to use permat functions to iterate custom functions over data frames. I'll also show data wrangling tips and tricks in practical examples of managing your data workflow. In my last video, I delivered the briefest possible regex introduction where I used regex to compute useful transformations in multi-value fields. This time I'm going to go a little deeper while striving to keep things practical. All right, pro tip. Starting out, use only enough regex to be dangerous. Recognize that you may be dangerously powerful, so you're going to check your results to make sure you're getting expected responses. All right, as always, see the video description for the link to the code in the GitHub repository. And let's get started. Regex are for finding patterns in text. Regex has been around since the 1950s. It's implemented broadly within nearly every computer application that offers a find and replace feature. Now, in complicating ways, regex is way more powerful than the find and replace feature most people are used to. Further complicated because the power of regex has slightly different syntactical implementations across programming languages. So the general purpose concepts are useful and important, but the syntactical implementations vary. At the same time, they're all pretty much the same. They're very similar, but it has a serious impact on your ability to get results. So you need to be aware of those syntactical differences. Now, since I prefer the tidyverse dialect of R, I'll show you a tidyverse approach to regex. So here's my approach. One, learn the basics of regex, just the basics. You can learn more as you go, but you don't really need to bog down at what all of regex can do. Two, use the stringer package part of the tidyverse, so stringer.tidyverse.org and leverage the stringer package to implement and use regex. Three, use the stringer cheat sheet to refine your pattern match compositions. All right, so let's just take a look at the basics. So here you see at line 22, a little table with some very basic symbols that are regex symbols. For example, in line 23, slash t is a way to indicate a digit. Similarly, at line 26, slash w is a way to indicate a word character. Now you can anchor a word character or a digit with what you see in lines 24 and 25. In other words, a dollar sign following slash d says a digit at the end of my string. A carrot at the beginning of a slash w says a word character at the beginning of my string. Now you can also do the inverse slash capital D and slash capital W. So for example, slash capital D says a non-digit slash capital W says a non-word character. But be careful to note, a digit is part of an alphanumeric string, so a slash capital W does not mean a digit. And these tips you'll learn along the way. All right, now you can have that regex pattern and you can add multipliers to it. So for example, at lines 27 and 28, a plus says one or more of my pattern, an asterisk says zero or more of my pattern. Similarly, there's a slash s for a space. There's a dot for a wild card. In other words, find everything would be dot asterisk, right? Lines 30 and lines 28. And then furthermore, you can put your regex patterns within parentheses to make capture groups that you can refer to later. All right, let's take a look here at line 36. You'll see that we're using the string extract function. You'll also see a number of other functions like string underscore count at line 42, which we already learned about in the previous video series. Also a new one here is string underscore replace that you see at line 44 and string underscore detect. And then of course at line 48, string underscore replace underscore all. Now there are many different string underscore functions and you can refer to the stringer website or the on-board help in R to see all of the stringer string underscore functions. All right, so let's just take a quick look at line 38 and 39. What we're saying there in the pattern is in 38, find me a digit at the end of my string and my string is height. What we're saying in line 39 is find me a digit at the beginning of my string. What we're saying in line 41 is find me a word character at the beginning of my string but not just a single word character, a multiplier of a plus, which says one or more, so multiple words or multiple word characters. In line 42, we're saying count the number of spaces and we can use that as a way to count the number of words in a name string. So if there are spaces separating names and we count the spaces and we add one, then we know how many names we have. For example, there's one space in Luke Skywalker. If we add one, we have two names. Luke as a first name, Skywalker as a last name. You can see in line 43 the pattern are within brackets and the pattern that we're looking for here are vowels. Notice that we have both lowercase and uppercase vowels because regex is case sensitive. Down at line 49 is an example of how you can tell regex to be case insensitive. In line 44, we see we're using the string underscore replace with capture groups. So the first capture group within parenthesis is a word character of multiples followed by a space followed by dot star within a capture group. So that's followed by anything. Now we have two capture groups that you see after the comma we can refer to by saying first give me the second capture group comma then a space quote slash slash two comma space slash slash one quote. So that's the pattern that we're returning. Finally, string detect allows us to return to false and the pattern that we're looking for there is a series of double consonants and you'll notice that there's a pipe character in between each one of those double consonants so that functions as a boolean or give me a tt or an ll or an ff etc. And then lastly we combine string detect and string replace all with if else so that we can change the value of one variable depending on the pattern we find in a different variable. Stay tuned for other videos and we'll show you more tips and tricks.