The Wayback Machine - https://web.archive.org/web/20240603220818/https://www.geeksforgeeks.org/perl-regular-expressions/
Open In App

Perl | Regular Expressions

Last Updated : 30 May, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

Regular Expression (Regex or Regexp or RE) in Perl is a special text string for describing a search pattern within a given text. Regex in Perl is linked to the host language and is not the same as in PHP, Python, etc. Sometimes it is termed as “Perl 5 Compatible Regular Expressions“. To use the Regex, Binding operators like ‘=~‘(Regex Operator) and ‘!~‘ (Negated Regex Operator) are used. Before moving to Binding operators let’s have a look at building patterns.

Building Patterns: In Perl, patterns can be constructed using the m// operator. In this operator, the required pattern is simply placed between the two slashes and the binding operators are used to search for the pattern in the specified string.

Using m// and Binding Operators: Mostly the binding operators are used with the m// operator so that required pattern could be matched out. Regex operator is used to match a string with a regular expression. The left-hand side of the statement will contain a string which will be matched with the right-hand side containing the specified pattern. Negated regex operator is used to check if the string is not equal to the regular expression specified on the right-hand side.

  • Program 1: To illustrate the use of ‘m//’ and ‘=~’ as follows:




    # Perl program to demonstrate
    # the m// and =~ operators
      
    # Actual String
    $a = "GEEKSFORGEEKS";
      
    # Prints match found if 
    # its found in $a
    if ($a =~ m[GEEKS]) 
    {
        print "Match Found\n";
    }
      
    # Prints match not found 
    # if its not found in $a
    else 
    {
        print "Match Not Found\n";
    }

    
    

    Output:

    Match Found
    
  • Program 2: To illustrate the use of ‘m//’ and ‘!~’ as follows:




    # Perl program to demonstrate
    # the m// and !~ operators
      
    # Actual String
    $a = "GEEKSFORGEEKS";
      
    # Prints match found if 
    # its not found in $a
    if ($a !~ m[GEEKS]) 
    {
        print "Match Found\n";
    }
      
    # Prints match not found 
    # if it is found in $a
    else
    {
        print "Match Not Found\n";
    }

    
    

    Output:

    Match Not Found
    

Uses of Regular Expression:

  • It can be used to count the number of occurrence of a specified pattern in a string.
  • It can be used to search for a string which matches the specified pattern.
  • It can also replace the searched pattern with some other specified string.


Previous Article
Next Article

Similar Reads

Perl | Special Character Classes in Regular Expressions
There are many different character classes implemented in Perl and some of them used so frequently that a special sequence is created for them. The aim of creating a special sequence is to make the code more readable and shorter. The Special Character Classes in Perl are as follows: Digit \d[0-9]: The \d is used to match any digit character and its
4 min read
Perl - Use of Capturing in Regular Expressions
A regular expression or a regex is a string of characters that define the pattern that we are viewing. It is a special string describing a search pattern present inside a given text. Perl allows us to group portions of these patterns together into a subpattern and also remembers the string matched by those subpatterns. This behaviour is known as Ca
3 min read
Perl | Basic Syntax of a Perl Program
Perl is a general purpose, high level interpreted and dynamic programming language. Perl was originally developed for the text processing like extracting the required information from a specified text file and for converting the text file into a different form. Perl supports both the procedural and Object-Oriented programming. Perl is a lot similar
10 min read
Perl | Backtracking in Regular Expression
In Perl, a Regular expression(a.k.a regexes or regexps or REs) is a way of describing a set of strings without having to list all strings in your program or simply we can say that it is a sequence of characters that are used for pattern matching. In Perl, regular expressions have different uses: Firstly, they are used in conditionals to determine w
3 min read
Perl | Quantifiers in Regular Expression
Perl provides several numbers of regular expression quantifiers which are used to specify how many times a given character can be repeated before matching is done. This is mainly used when the number of characters going to be matched is unknown. There are six types of Perl quantifiers which are given below: * = This says the component must be prese
4 min read
Perl | Operators in Regular Expression
Prerequisite: Perl | Regular Expressions The Regular Expression is a string which is the combination of different characters that provides matching of the text strings. A regular expression can also be referred to as regex or regexp. The basic method for applying a regular expression is to use of binding operators =~ (Regex Operator) and !~ (Negate
4 min read
Perl | pos() function in Regular Expression
pos() function in Perl is used to return the position of the last match using the 'm' modifier in Regex. pos function can be used with the character classes in Regex to return a list of all the required substring positions in the given string. Global operator 'g' can also be used along with the 'm' modifier to search for the substring within the wh
3 min read
Perl | 'e' modifier in Regular Expression
In Perl, the regular expression allows performing various operations on a given string with the use of suitable operators. These operators can perform operations like modification of string, substitution of other substrings, etc. Substitution of a substring in the given string is done with the use of 's'(substitution) operator, which takes two oper
3 min read
Perl | chomp() Function
The chomp() function in Perl is used to remove the last trailing newline from the input string. Syntax: chomp(String) Parameters: String : Input String whose trailing newline is to be removed Returns: the total number of trailing newlines removed from all its arguments Example 1: C/C++ Code #!/usr/bin/perl # Initialising a string $string = &quo
2 min read
Perl | Searching in a File using regex
Prerequisite: Perl | Regular Expressions Regular Expression (Regex or Regexp or RE) in Perl is a special text string for describing a search pattern within a given text. Regex in Perl is linked to host language and are not the same as in PHP, Python, etc. Sometimes these are termed as "Perl 5 Compatible Regular Expressions". To use the Regex, Bindi
6 min read
Article Tags :