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.
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:
The following output appears after executing the script with the “1080” input value:
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.
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:
The following output apears after executing the script with the “xray” input value:
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.
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:
The following output appears after executing the script with the “PHP programming” input value:
Conclusion
Simple uses of the regex in Perl are shown in this tutorial using multiple Perl examples to help the new Perl users.