2. Contents
■ Regex
■ Regex SpecialCharacters
■ Regex Example
■ Square brackets
■ Pattern Matching & Examples
■ File I/O
■ Open a File
■ Different Methods of Files
■ Reading &Writing and Append the Files
■ Closing the Files
3. What is Regex.?
■ Regex is a string of text that allows you to create patterns that
help match, locate, and manage text.
■ Perl is a great example of a programming language that utilizes
regular expressions.
■ Regular Expressions can also be used from the command line
and in text editors to find text within a file.
4. RegexSyntax
■ Regex was most popular with perl.
■ Regex are build in to perl.
■ However almost every language use regex.
■ Systax could have minor differences from
language to language.
Regex
code
Perl
compiler
outputregex engine
5. RegexSyntax
■ Regular expressions are used to perform pattern-matching and
“search-and-replace” functions on text.
■ Regex syntax:-
■ /pattern/modifiers
■ Creating a regular exp:-
■ var re = /pattern/modifiers ; or var re = new RegExp(”pattern”,
”modifiers”)
■ Modifiers
■ i,g,m
13. What is File input and output
■ Perl to read and update the data stored within the data stream
associated with the file handle.
■ A file handle is a named internal Perl structure that associates a
physical file with a name.
■ All file handles are capable of read/write access, so you can read
from and update any file or device associated with a file handle.
■ Three basic file handles are - STDIN, STDOUT,
and STDERR, which represent standard input, standard output
and standard error devices respectively.
14. Opening and Closing Files
■ There are following two functions with multiple forms, which can
be used to open any new or existing file in Perl.
■ Here FILEHANDLE is the file handle returned by the open function
and EXPR is the expression having file name and mode of opening
the file.
15. Opening Files
■ Opening files will used to open() function.
■ Following is the syntax to open file.txt in read-only mode.
■ Here DATA is the file handle, which will be used to read the file.
Here is the example, which will open a file and will print its
content over the screen.
17. Closing Files
■ To close a file handle, and therefore disassociate the file handle
from the corresponding file, you use the close function.
■ If no FILEHANDLE is specified, then it closes the currently
selected file handle. It returns true only if it could successfully
flush the buffers and close the file.
19. Reading and Writing and Append Files
■ Once you have an open file handle, you need to be able to read
and write information.
■ There are a number of different ways of reading and writing data
into the file.
20. Reading Files
■ Consider we have a Perl file with name file.txt and has few lines
of text in it.We need to open this file and print the same.
■ $_ The default variable without parameter.
21. Writing Files
■ Now that you know how to open and read files learning how to
write to them is straightforward.
22. Appending Files
■ The above way will always try to create a file named test.txt and
writes the input into the file. we will write the same to append
the file.
23. Copying Files
■ Here is the example, which opens an existing file file1.txt and
read it line by line and generate another copy file file2.txt.
24. Renaming Files
■ Here is an example, which shows how we can rename a file
file_rename.txt to file_name.txt.
25. Creating Files
■ Here is an example, which shows how we can Create a file inside
a folder. If file doesn't Exists.
26. Deleting an Existing File
■ Here is an example, which shows how to delete a file filedel.txt
using the unlink function.