简体   繁体   English

Perl - 如果字符串包含文本?

[英]Perl - If string contains text?

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.我想使用 curl 来查看页面的源,如果该源包含与字符串匹配的单词,那么它将执行打印。 How would I do a if $string contains ? if $string contains我该怎么做?

In VB it would be like.在VB中它会像。

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

Something similar to that but in Perl.与此类似,但在 Perl 中。

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):如果您只需要在另一个字符串中搜索一个字符串,请使用index rindex (如果您想从字符串末尾开始扫描,则使用 rindex):

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

To search a string for a pattern match, use the match operator m// :要在字符串中搜索模式匹配,请使用匹配运算符m//

if ($string =~ m/pattern/) {
    print "'$string' matches the pattern\n";       
}
if ($string =~ m/something/) {
   # Do work
}

Where something is a regular expression. something是正则表达式。

For case-insensitive string search, use index (or rindex ) in combination with fc .对于不区分大小写的字符串搜索,将index (或rindex )与fc结合使用。 This example expands on the answer by Eugene Yarmash:此示例扩展了 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.如果未找到 substring,则indexrindex都返回-1
And fc returns a casefolded version of its string argument, and should be used here instead of the (more familiar) uc or lc . fc返回其字符串参数的大小写版本,应该在这里使用而不是(更熟悉的) uclc Remember to enable this function, for example with use feature qw( fc );请记住启用此 function,例如use feature qw( fc ); . .

DETAILS:细节:

From the fc docs:来自fc文档:

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 :来自Unicode 常见问题解答

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. Unicode 中的大小写折叠主要基于小写映射,但包括对源文本的额外更改以帮助使其对语言不敏感且保持一致。 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.因此,大小写折叠文本应仅用于内部处理,通常不应存储或显示给最终用户。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM