Perl - If string contains text?

StringPerlString Matching

String Problem Overview


I want to use curl to view the source of a page and if that source contains a word that matches the string then it will execute a print. How would I do a if $string contains?

In VB it would be like.

dim string1 as string = "1"
If string1.contains("1") Then
Code here...
End If

Something similar to that but in Perl.

String Solutions


Solution 1 - String

If you just need to search for one string within another, use the index function (or rindex if you want to start scanning from the end of the string):

if (index($string, $substring) != -1) {
   print "'$string' contains '$substring'\n";
}

To search a string for a pattern match, use the match operator m//:

if ($string =~ m/pattern/) {  # the initial m is optional if "/" is the delimiter
    print "'$string' matches the pattern\n";       
}

Solution 2 - String

if ($string =~ m/something/) {
   # Do work
}

Where something is a regular expression.

Solution 3 - String

For case-insensitive string search, use index (or rindex) in combination with fc. This example expands on the answer by Eugene Yarmash:

use feature qw( fc ); 
my $str = "Abc"; 
my $substr = "aB"; 

print "found" if index( fc $str, fc $substr ) != -1;
# Prints: found

print "found" if rindex( fc $str, fc $substr ) != -1;
# Prints: found

$str = "Abc";
$substr = "bA";

print "found" if index( fc $str, fc $substr ) != -1;
# Prints nothing

print "found" if rindex( fc $str, fc $substr ) != -1;
# Prints nothing

Both index and rindex return -1 if the substring is not found.
And fc returns a casefolded version of its string argument, and should be used here instead of the (more familiar) uc or lc. Remember to enable this function, for example with use feature qw( fc );.

DETAILS:

From the fc docs:

> Casefolding is the process of mapping strings to a form where case differences are erased; comparing two strings in their casefolded form is effectively a way of asking if two strings are equal, regardless of case.

From the Unicode FAQ:

> Q: What is the difference between case mapping and case folding? > > A: Case mapping or case conversion is a process whereby strings are > converted to a particular form—uppercase, lowercase, or > titlecase—possibly for display to the user. Case folding is mostly > used for caseless comparison of text, such as identifiers in a > computer program, rather than actual text transformation. Case folding > in Unicode is primarily based on the lowercase mapping, but includes > additional changes to the source text to help make it > language-insensitive and consistent. As a result, case-folded text > should be used solely for internal processing and generally should not > be stored or displayed to the end user.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionHellosView Question on Stackoverflow
Solution 1 - StringEugene YarmashView Answer on Stackoverflow
Solution 2 - StringSean BrightView Answer on Stackoverflow
Solution 3 - StringTimur ShtatlandView Answer on Stackoverflow