Home » Perl Basics » 10 - Regular Expressions
Regular expressions are a vast subject. We gave you the basics in that chapter. Experiment with it as much as you can so you are comfortable in searching for anything you need within a script or a file. Besides experimenting looking at other scripts is very helpful in finding new/better way to do what you already know how to do. There are a vast number of scripts online for you to look at. Those scripts, especially the more complex ones, are not here just to teach you. Most of them you can reuse freely. By reusing other’s script you will be able to save time and have a running script faster. In the next chapter we will see how to use modules.
10
Regular Expressions Example
Further explanation and examples of regular expressions.
Perl will most of the time be dealing with files. In that example we will use file I/O and regular expressions to extract information from the file.
open ( INFILE, “name-of-the-file”) || die “error opening file $!”;
while (<INFILE> {
If (/alert|warning/i) {
print “$_”;
}
This script will extract all the lines in the files that contain “alert” and/or “warning” without any regard for the case.Regular expressions are a vast subject. We gave you the basics in that chapter. Experiment with it as much as you can so you are comfortable in searching for anything you need within a script or a file. Besides experimenting looking at other scripts is very helpful in finding new/better way to do what you already know how to do. There are a vast number of scripts online for you to look at. Those scripts, especially the more complex ones, are not here just to teach you. Most of them you can reuse freely. By reusing other’s script you will be able to save time and have a running script faster. In the next chapter we will see how to use modules.