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:
- 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:# 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_rightfilter_noneOutput:
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:# 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_rightfilter_noneOutput:
chocolate cake
- 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.
- 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:# 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_rightfilter_noneOutput:
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:
# 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_rightfilter_noneOutput:
chocolate cake
- Positive Lookbehind(?<=pattern):Positive Lookbehind, it makes sure that the pattern does exist in the given string we are matching.
Recommended Posts:
- Perl | 'ee' Modifier in Regex
- Perl | Anchors in Regex
- Perl | Searching in a File using regex
- Perl | Grouping and Alternation in Regex
- Perl | Regex Character Classes
- Perl | Regex Cheat Sheet
- Perl | Extract IP Address from a String using Regex
- Perl Tutorial - Learn Perl With Examples
- Perl | Basic Syntax of a Perl Program
- How to get YouTube video ID with PHP Regex ?
- Function to escape regex patterns before applied in PHP
- Split a String into columns using regex in pandas DataFrame
- Perl vs C/C++
- Perl | tell() Function
- Perl | Modules
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.