Updated April 6, 2023
Introduction to Perl Regular Expression
Perl regular expression is used to define the pattern of string or character in Perl, using regular expression in Perl we have to define the pattern of any string or character. The regular expression is similar to the awk, grep, and sed command in a shell script or Linux system, the syntax is also similar to the awk, grep, and sed command. The regular expression is very important in Perl to define the pattern of any string or character. The basic method is available to apply regular expression in Perl is to use the pattern binding operator =~ and !~, the first operator is the assignment operator.
How Regular Expression works in Perl?
Below is the working of regular expression in Perl is as follows. Mainly there are three regular expressions operators is available in Perl as follows.
- Matching regular expression operator
- Substitute regular expression operator
- Transliterate regular expression operator
1. The matching operator in Perl
Below is the matching operator modifiers available in Perl are as follows.
- cg – It is used to continue the search even global match failed.
- g – It is used to search for all matches.
- i – It is used to search all matches with case insensitivity.
- m – It is used with a new line boundary instead of a sting boundary.
- o – It is used to allow expression evaluation only once in Perl.
- s – It is used to match the newline character in Perl.
- x – It is used white space in the expression.
We have used Perl matching operator =~ and !~ to match the word given from the string.
2. Substitution operator in Perl
- Substitution operator in Perl just the extension of a matched operator.
- It is used to match the replacement of text matched with the new text which we have used in code.
- Below is the syntax of the substitution operator in Perl is as follows.
Syntax
s/old_pattern/new_pattern/;
3. Translation operator in Perl
- The translation operator is similar to the substitution operator in Perl. But translation is not used in regular expression or search on replacement values.
- Below is the syntax of the translation operator in Perl is as follows.
Syntax –
tr/old_letter/new_letter/;
- Transition and substitution is similar to sed command which was used in a Linux.
- The regular expression in Perl is the pattern that provided a flexible concise mean to match a string in Perl.
- The regular expression in Perl also referred or known as regexp or regex.
- The regular expression is similar to the awk, grep, and sed command in a shell script or Linux system, the syntax is also similar to the awk, grep, and sed command.
- Perl regular expression is used to define the pattern of string or character in Perl. Using regular expression in Perl we have to define the pattern of any string or character.
- The basic method is available to apply regular expression in Perl is to use the pattern binding operator =~ and !~, the first operator is the assignment operator.
- The regular expression is very important and useful in Perl to define the pattern of any string or character.
- The regular expression in Perl can be either complex or simple. It depends on the pattern which we have used in code.
- We can construct the regular expression pattern using m operator. In this pattern of regular expression required pattern was simply placed between the two slashes and the binding operator.
- Before using the binding operator in Perl we need to define a building pattern in Perl. The binding operator is very important and useful in Perl.
- We have mostly use the binding operator with the m operator in Perl. Because it is suitable to match the required pattern.
- A regular expression operator is mostly useful to match the string with the regular expression in Perl.
- In a regular expression, the left-hand side statement will contain a string that was matched with the pattern of the right-hand side.
- In Perl, regular expression negated regex operator is used to checking the string is not equal to the regular expression which was specified on the right-hand side.
Examples to implement Regular Expression in Perl
Below is the example are as follows.
Example #1. Perl matching operator (=~)
The matching operator is used to match a word with the given string. The below example shows the matching operator is as follows.
In the below example we have used string name as “matching operator Perl”.
Code:
$line = "Matching operator perl.";
if ($line =~ /perl/){
print "Matched\n";
} else {
print "Not Matched\n";
}
if ($line =~ /Linux/){
print "Matched\n";
} else {
print "Not Matched\n";
}
Output:
Example #2. Perl matching operator (!~)
This operator is opposite to the operator of =~ in Perl. The below example shows the matching operator (!~) are as follows.
In the below example we have used string name as “matching operator Perl”.
Code:
$line = "Matching operator perl.";
if ($line !~ /perl/){
print "matched\n";
} else {
print "Not Matched\n";
}
if ($line !~ /Linux/){
print "Matched\n";
} else {
print "Not Matched\n";
}
Output:
Example #3. Perl matching operator ($_)
We can match the string with $_ in Perl. The below example shows the matching operator ($_) is as follows.
In the below example we have used string name as “matching operator Perl”.
Code:
$_ = "Matching operator perl.";
if (/perl/) {
print "Matched\n";
}
else {
print "Not Matched\n";
}
if (/Perl/) {
print "Matched\n";
}
else {
print "Not Matched\n";
}
Output:
Example #4. Perl substitution operator
Perl substitution operator is an extension of a matching operator in Perl. The below example shows the Perl substitution operator is as follows.
We have substitute value using s/ /; in Perl.
Code:
$sub_line = "perl is programming language.";
$sub_line =~ s/programming/scripting/;
print "$sub_line\n";
print"\n";
$sub_line = "perl is scripting language.";
$sub_line =~ s/programming/scripting/g;
print "$sub_line\n";
Output:
Example #5. Perl translation operator
The translation operator is similar to the substitution operator in Perl. The below example shows the Perl substitution operator is as follows.
Code:
$tr_line = "perl is known as scripting language.";
$tr_line =~ tr/l/z/;
print "$tr_line\n";
Output:
Conclusion
The regular expression is very important and useful in Perl to define the pattern of any string or character. The regular expression is similar to the awk, grep, and sed command in a shell script or Linux operating system. Regular expression syntax is also similar to the awk, grep, and sed command.
Recommended Articles
We hope that this EDUCBA information on “Perl Regular Expression” was beneficial to you. You can view EDUCBA’s recommended articles for more information.