Writing logical and efficient alternations


show more Writing logical and efficient alternations provides you with in-depth training on Developer. Taught by Kevin Skoglund as part of the Using Regular Expressions show less
please wait ...

Writing logical and efficient alternations

Now that we have the basics of working with alternations, I want us to dive a little bit deeper and make sure that we write logical and efficient alternations. First, keep in mind the basic principals of regular expressions that we've seen so far, that they're eager and they're greedy. Those have an impact on the way that it process alternations. Let's go into regexpal, and let's start by just putting in peanut butter, and then up here let's put in either peanut|peanutbutter. Notice that it matched peanut, not peanut butter.

That's because it's eager. It's eager to return a result, and as we saw before, the leftmost item gets priority. Therefore, it's going to prefer to match the first item and never even attempt the second expression at all. Now if you wanted it to return peanut butter, we saw how to do that before by using our option. Let's put in this. There it is. Now it uses peanut and then butter is optional, but butter is preferred because,...

Writing logical and efficient alternations
Video duration: 7m 33s 5h 36m Intermediate

Viewers:

Writing logical and efficient alternations provides you with in-depth training on Developer. Taught by Kevin Skoglund as part of the Using Regular Expressions

Subject:
Developer
Software:
Regular Expressions
Author:
please wait ...