Using Regular expressions
There is nothing that can save your time whole searching for files like the use of regular expressions. They are not only used in the search of files but the message is that they are the real saviours when things are hot. Servers can have a multitude of files to search for, thousands of lines in logs to look out for and this is where the inventors of regular expressions receive sincere credit. Let us dive in and learn how they can be leveraged to boost your experience in the near future. First and foremost, there are two types of regular expressions that you should know: that is the basic and the extended forms. The differences between the two are quite subtle and where they are to be applied will highly depend on the program such as grep or sed or others. This article will not tell more about that but we shall explore them in another lesson.
Simple regular expressions.
Some of the simplest forms of regular expressions are the use of strings such as “hope” or “faith.” A good example is like this:
“It is pretty hopeful that the light at the end of the tunnel is our turn to reap”
When the word hope is being searched in such a sentence, then the regular expression to be used is “hope” because it will match any string whether long or of similar size to the regular expression. That is a simple example of the use of regular expression because instead of reading the whole sentence to find the word “hope” , the regular expression will bring it out instantly for your particular application.
Advanced and more powerful regular expressions use nonalphabetic characters. These characters open a powerful door that regular expressions inherently posses. The following are the matching rules in this advanced form:
Single characters. The use of the dot (.) will represent any character whether alphabetic or numerical. For instance, a.g will match any word like a5g, aag, apg, aRg and much more as long as the word begins with a and ends with g.
Square bracket expressions:
m[aeiou]: the bracket expression will match either mat, met, mit, mot or mut..
Use of range expression:
p[6-9]g: this will match p6g, p7g,p8g, p9g. It will use the range given inside the square brackets to match the given task. It is a flavor of the square bracket expression..
Finding the start and the end of a line: the carat (^) and the dollar($) are used to denote the start and the end of a line respectively.
Matching many strings: the use of the or (|) operator is used when two or more strings are to be matched. For instance to match hope and faith, you do: faith|hope
Use of (*,?and +): and asterisk is used to denote null or more occurrences, a plus (+)is used to match a singe one or more occurrences while the question mark (?) is used to denote null or one match only.
Example: spart.* will match spartan, sparter, spart, spartoo, sparcko and much more.
Escaping: a good question may arise like this, how can special characters or words that have special characters such as a dot be matched? A good answer will be by escaping them with the use of the back slash character. An example is matching a URL such as as you can see, the URL has got dots within it. To match it, escape characters are used this way:
Tune in for our next lesson as we find some of the widely used programs that use regular expressions and their associated outcomes. The programs to be examined are grep and sed.