
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Count the Number of Matches in a Perl String
In Perl, we can find the number of matches in a string by different approaches. In this tutorial, we will discuss the three most widely used approaches.
Searching for a Single Character in a Perl String
Let's first take the case where we would want to search for a single character pattern in a string. For example, let's suppose we have a string that look something like this ?
"India.Japan.Russia.USA.China"
And, we want to find the number of times "." (dot) appears in the above string.
Example
Consider the code shown below.
my $countries = "India.Japan.Russia.USA.China"; print "The given string is: ", $countries; print "\n"; my @countDotOccurrences = $countries =~ /\./g; print "Number of dots in the string: ", scalar @countDotOccurrences;
Output
If you run the above code in a Perl compiler online, then you will get the following output in the terminal ?
The given string is: India.Japan.Russia.USA.China Number of dots in the string: 4
Example
Now let's take another approach that will also allow us to find the occurrence of a single character in a Perl string. Consider the code shown below.
$someString = "Perl Codes Can Be Difficult To Read"; print "The given string is: ", $someString; print "\n"; $countT = ($someString =~ tr/T//); $countX = ($someString =~ tr/X//); print "$countT T characters in the string\n"; print "$countX X characters in the string";
Output
If you run the above code in a Perl compiler, then you will get the following output in the terminal ?
The given string is: Perl Codes Can Be Difficult To Read 1 T characters in the string 0 X characters in the string
As shown in the output, the given string has 1 "T" and no "X" characters. Note that we are searching for capital letters only.
Searching for Multiple Characters in a Perl String
In the above two examples, we explored the case where we would want the occurrence of a single character in a string. In this example, we would explore how to search multiple characters.
Example
Consider the code shown below. Here, we have a string with some positive and negative numbers. We will find out how many negative numbers are there in the given string.
$someString = "-9 57 48 -2 -33 -76 4 13 -44"; print "The given string is: ", $someString; print "\n"; while ($someString =~ /-\d+/g) { $negativeCount++ } print "There are $negativeCount negative numbers in the string.";
Output
If you run the above code in a Perl compiler online, you will get the following output in the terminal ?
The given string is: -9 57 48 -2 -33 -76 4 13 -44 There are 5 negative numbers in the string.
Conclusion
In this tutorial, we used multiple examples to show how you can count the number of matches in a Perl string.