Perl | Assertions in Regex

Regular Expression (Regex or RE) in Perl is when a special string describing a sequence or the search pattern in the given string.
An Assertion in Regular Expression is when a match is possible in some way. The Perl’s regex engine evaluates the given string from left to right, searching for the match of the sequence, and when we found the match sequence at a given point in a string that position is called as match position or current match position. The Look Around Assertions facilitates us to match a pattern or a string before or after the match position or current match position without changing the current match position.

Types of Assertions:
There are mainly two types of Assertions in Regex which are further divided:
Types of Assertions

  1. Lookahead Assertions: In this type of assertion we look ahead the current match position, means will search the string after the current match position or the pattern/string succeeding string, without moving the current match position.

    Syntax for Lookahead Assertions:



    /(?=pattern)/

    Lookahead Assertions can be further classified into:

    • Positive Lookahead(?=pattern): Positive Lookahead is just like the normal Lookahead assertion, it makes sure that the pattern does exist in the given string which we are matching.
      Example:

      filter_none

      edit
      close

      play_arrow

      link
      brightness_4
      code

      # Perl code for demonstrating 
      # Positive Lookahead Modules used 
      use strict; 
      use warnings; 
        
      $_ = "chicken cake";
        
      # It will check that if the cake is
      # in the string, then 
      # replace the chicken with chocolate
      s/chicken (?=cake)/chocolate /;
        
      # printing the result
      print $_;

      chevron_right

      
      

      Output:

      chocolate cake
    • Negative Lookahead(?!pattern): In Negative Lookahead, it Lookaheads the pattern and makes sure that the pattern doesnot exist in the given string which we are matching. To make Lookahead assertion as Negative Lookahead we just replace the ‘='(Equal) with ‘!'(Exclamation).
      Example:

      filter_none

      edit
      close

      play_arrow

      link
      brightness_4
      code

      # Perl code for demonstrating 
      # Negative Lookahead Modules used 
      use strict; 
      use warnings; 
        
      $_ = "chicken cake";
        
      # it will check that if the curry is not
      # in the given string, then
      # replace the chicken with chocolate
      s/chicken (?!curry)/chocolate /;
        
      # printing the result
      print $_;

      chevron_right

      
      

      Output:

      chocolate cake
  2.  

  3. Lookbehind Assertions: In this type of assertion we look behind the current match position, means will search the string before the current match position or the preceding string, without moving the current match position.
    Syntax for Lookbehind Assertions:

    /(?<=pattern)/

    Lookbehind Assertions can be further classified into:

    • Positive Lookbehind(?<=pattern):Positive Lookbehind, it makes sure that the pattern does exist in the given string we are matching.
      Example:

      filter_none

      edit
      close

      play_arrow

      link
      brightness_4
      code

      # Perl code for demonstrating 
      # Positive Lookbehind Modules used 
      use strict; 
      use warnings; 
        
      $_= "chocolate curry";
        
      # it will check that if the chocolate 
      # is preceding curry, then 
      # it will replace the curry with cake
      s/(?<=chocolate )curry/cake /;
        
      # printing the result 
      print $_;

      chevron_right

      
      

      Output:

      chocolate cake
    • Negative Lookbehind(?<!pattern):Negative Lookbehind, it Lookbehind the pattern and make sure that the pattern does not exist in the given string we are matching. To make Lookbehind assertion as negative we just replace the ‘='(Equal) with ‘!'(Exclamation).

      Example:

      filter_none

      edit
      close

      play_arrow

      link
      brightness_4
      code

      # Perl code for demonstrating 
      # Negative Lookbehind Modules used 
      use strict; 
      use warnings; 
        
      $_= "chocolate curry";
        
      # it will check that if the chicken
      # is not preceding curry, then 
      # it will replace the curry with cake
      s/(?<!chicken )curry/cake /;
        
      # printing the result 
      print $_;

      chevron_right

      
      

      Output:

      chocolate cake


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.