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

发布于 2024-11-29 00:34:01 字数 238 浏览 2 评论 0原文

我想使用curl来查看页面的源代码,如果该源代码包含与字符串匹配的单词,那么它将执行打印。我该如何执行if $string contains

在VB中会是这样的。

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

与此类似,但在 Perl 中。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

红ご颜醉 2024-12-06 00:34:01

如果您只需要在另一个字符串中搜索一个字符串,请使用 index< /a> 函数(或者 rindex 如果你想开始从字符串末尾扫描):

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

搜索字符串对于模式匹配,请使用匹配运算符m//

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

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";       
}
辞取 2024-12-06 00:34:01
if ($string =~ m/something/) {
   # Do work
}

其中 something 是正则表达式。

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

Where something is a regular expression.

暮色兮凉城 2024-12-06 00:34:01

对于不区分大小写的字符串搜索,请使用 index (或rindex) 与 fc。此示例扩展了 Eugene Yamash 的答案:

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

如果未找到子字符串,indexrindex 都会返回 -1
fc 返回其字符串参数的大小写版本,应该在这里使用,而不是(更熟悉的)uclc。请记住启用此功能,例如使用 use feature qw( fc );

详细信息:

来自 fc 文档:

大小写折叠是将字符串映射到消除大小写差异的形式的过程;比较大小写形式的两个字符串实际上是询问两个字符串是否相等(无论大小写)的一种方法。

来自 Unicode 常见问题解答

问:大小写映射和大小写折叠有什么区别?

A:大小写映射或大小写转换是字符串转换的过程
转换为特定形式——大写、小写或
titlecase——可能用于向用户显示。箱体折叠大部分是
用于文本的无大小写比较,例如文本中的标识符
计算机程序,而不是实际的文本转换。表壳折叠
Unicode 中主要基于小写映射,但包括
对源文本进行额外更改以帮助实现
语言不敏感且一致。结果,大小写折叠的文本
应仅用于内部处理,一般不应
被存储或显示给最终用户。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文