

For instance, using regular expressions, you could find all the instances of the word cat in a document, or all instances of a word that begins with c and ends with t.

applies to the preceding item which is the range.A regular expression (also called a regex or regexp) is a rule that a computer can use to match characters or groups of characters within a larger body of text.+ - the preceding character matches 1 or more times.* - the preceding character matches 0 or more times.? - the preceding character matches 0 or 1 times only.I will outline the basic building blocks of re's below then follow on with a set of examples to demonstrate their usage. It can be a good way to better understand how they work. Looking at what we actually did match and using it to help understand what actually did happen will help us to work out what we should try changing to get closer to what we actually want.ĭebuggex is an on-line tool that allows you to experiment with regular expressions and allows you to visualise their behaviour. Your current pattern may not have worked the way you want but we can still learn from it. This will help you to get a feel for which parts of the pattern is right and which parts you need to adjust. Break your pattern down into individual components and test each of these individually.Maybe what you thought a particular operator did was slightly different to what it actually does and re reading you will notice a point you may have missed the first time. If you're like me then you're prone to making them. If you're not getting the output you would like then here are some basic strategies. Remember you may hit the up arrow on your keyboard to get at your recent commands and also modify them so you don't need to retype the whole command each time. Don't worry if this happens you haven't done any damage and you can easily go back and have another go. When this happens typically every line will be matched or no lines will be matched or some obscure set. It is common to make mistakes in your patterns while you are learning. The best way to learn regular expressions is to give the examples a try yourself, then modify them slightly to test your understanding. If you are interested then I highly recommend going through our regular expression tutorial which goes into more detail. I'll give you an introduction to them here in this section but there is much more they can do. We will be demonstrating them here with grep but many other programs use them (including sed and vi which you learned about in previous sections) and many programming languages make use of them too. we may wish to identify every line which contains an email address or a url in a set of data. Re's are typically used to identify and manipulate specific pieces of data. Regular expressions are similar to the wildcards that we looked at in section 7. Mastering re's just takes practice and time so don't give up. You will be surprised but it will start to make more sense the second time. I find the best approach is to go over the material and experiment on the command line a little, then leave it for a day or 3, then come back and have another go. Re's can be a little hard to get your head around at first so don't worry if this stuff is a little confusing.

In this section we will look at another filter which is quite powerful when combined with a concept called regular expressions or re's for short. In the previous section we looked at a collection of filters that would manipulate data for us.

Discover the power of grep and regular expressions with this easy to follow beginners tutorial with plenty of examples to guide you.
