Perl Programming - SPLessons

Perl Regular Expression

Home > Lesson > Chapter 8
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Perl Regular Expression

Perl Regular Expression

shape Introduction

This chapter demonstrates about the Perl Regular Expression which is used to match the string pattern within a statement or group of statements and following are the concepts covered in this chapter.
  • Regular Expressions and Operators

Regular Expressions and Operators

shape Description

Perl regular expression is strong in matching the string patterns within a statements or group of statements. Regular expressions are mostly utilized in text parsing, pattern matching and plenty of more based on the need. Here we have some certain operators, which are specially used by regular expression pattern binding =~ and !~, These are test and assignment operators. Regular Expression Operator
  • Perl Match --m//
  • Perl Substitute -s///
  • Perl Transliterate -tr///
Following are the certain things like Meta characters, wild cards in Perl Regex syntax which are demonstrated in below table.

shape Examples

The example below demonstrates to user need to provide some input during the script execution and user need to check whether the user entered some name input or not. [perl] my $userinput="Guru99 Rocks"; if($userinput=~m/.*(Guru99).*/) { print "find pattern"; } else { print "unable to find the pattern"; } [/perl] Result By running the above code in a Perl command line user can get the following output as shown in below image.

Perl Match Operator

shape Description

Perl Match operator is used to match the strings in some statements or in a variable The code below demonstrates to print 'true', as the Perl pattern matching identifies the string in the variable. basically, Perl searches for the text provided in // throughout the string, even if it finds in one place it will return 'true'. The pattern can be anywhere in the variable. then user can try replacing =~ with !~ to see the difference between those two operators. [perl] my $var="Hello this is perl"; if($var=~m/perl/) { print "true"; } else { print "False"; } [/perl] Result By running the above code in a Perl command line user can get the following output as shown in below image.

Perl Translation Operator

shape Description

Perl Translation Operator is similar to substitution which does not use any regular expression rather then directly pass the value or word which user want to replace. The code below demonstrates the Perl Transition Operator. [perl] my $a="Hello SPLESSONS"; $a=~tr/hello/cello/; print $a; [/perl] Result By running the above code in a Perl command line user can get the following output as shown in below image.

Summary

shape Key Points

  • Regulr expressions are very strong while matching the string.
  • Regular expression operators are used to regular expression pattern binding.
  • Match Operators are used to match the strings.