Perl

Perl Regex Examples

The particular value can be matched or searched and replaced using regular expression patterns through Perl script. Different types of quantifiers, metacharacters, and character classes are used to define the regex patterns for matching and replacing the content of a string or file. The purposes and the uses of some commonly used quantifiers, metacharacters, and character classes for writing the regular expressions in the Perl script are shown in this tutorial.

Some Regex Quantifiers

Some commonly used quantifiers to write the regex patterns are mentioned in the following:

Quantifier Purpose
+ It is used to match one or more occurrences of the character.

Example: x+

* It is used to match zero or more occurrences of the character.

Example: x*

? It is used to match zero or one occurrence of the character.

Example: x?

{N} It is used for matching the exact number of times.

Example: x{5}

{N, } It is used for matching the minimum number of times.

Example: x{2, }

{N, M} It is used for matching the minimum and the maximum number of times.

Example: x{2, 10}

Some Regex Meta Characters

Some commonly used metacharacters to write the regex patterns are mentioned in the following:

Character Purpose
^ It is used to match the character at the beginning of the string.
$ It is used to match the character at the end of the string.
. It is used to match any character in the string except the new line.
\ It is used to match the special character and quotes in the string.

Some Regex Character Classes

Some commonly used character classes to write the regex patterns are mentioned in the following:

Class Purpose
[a-z] It is used to match all lowercase alphabets.
[A-Z] It is used to match with all uppercase alphabets.
[0-9] It is used to match all digits.
\d It is used to match all digits like [0-9].
\D It is used to match all characters except digits.
\w It is used to match all alphabets and digits.
\W It is used to match all characters except alphabets and digits.

Different Examples of Perl Regex

The uses of regex in Perl to match the particular characters and search and replace a string from a text are shown in this part of the tutorial.

Example 1: Search the Digit in the Input Value

The “\d” character class is used in this example to check whether the input that is taken from the user contains digits or not. If the “if” condition returns true, a success message is printed. Otherwise, the failure message is printed.

#!/usr/bin/perl

use strict;

use warnings;

#Take input from the user

print "Enter a string value: ";

my $input = <>;

chomp($input);

#Check whether the input value contains a digit or not

if ($input =~ /[\d]/)

{

  print "'$input' contains digits.\n";

}

else

{

  print "'$input' does not contain digit.\n";

}

Output:

The following output appears after executing the script with the “Hello” input value:

p1-1

The following output appears after executing the script with the “1080” input value:

p1-2

Example 2: Search the Characters of a Range in the Input Value

The “/[c-n]” character class is used in this example to check whether the input that is taken from the user contains any characters from “c” to “n” or not. If the “if” condition returns true, a success message is printed. Otherwise, the failure message is printed.

#!/usr/bin/perl

use strict;

use warnings;

#Take input from the user

print "Enter a string value: ";

my $input = <>;

chomp($input);

#Check whether the input value contains any character from 'c' to 'r' or not

if ($input =~ /[c-n]/)

{

  print "'$input' is valid.\n";

}

else

{

  print "'$input' is not valid.\n";

}

Output:

The following output appears after executing the script with the “lion” input value:

p2-1

The following output apears after executing the script with the “xray” input value:

p2-2

Example 3: Search and Replace the Word from the Input Value

The “s/search/replace/i” pattern is used in this example to search the word “Bash” and replace it with the word “Perl” if any part of the input matches with the word “Bash” in a case insensitive manner.

#!/usr/bin/perl

use strict;

use warnings;

#Take input from the user

print "Enter a string value: ";

my $input = <>;

chomp($input);

#Store the current input value to the user

my $previous_str = $input;

#Replace the content of the input value if the matching word found

$input =~ s/Bash/Perl/i;

#Check whether the input string is modified or not

if($input ne $previous_str)

{

  print "Replaced string is $input.\n";

}

else

{

  print "No Matching word found.\n";

}

Output:

The following output appears after executing the script with the “bash programming” input value:

p3-1

The following output appears after executing the script with the “PHP programming” input value:


p3-3

Conclusion

Simple uses of the regex in Perl are shown in this tutorial using multiple Perl examples to help the new Perl users.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.