简体   繁体   English

如何使用正则表达式匹配两个不同变量中的两个字符串?

[英]how do you match two strings in two different variables using regular expressions?

$a='program';
$b='programming';
if ($b=~ /[$a]/){print "true";}

this is not working 这不起作用

thanks every one i was a little confused 谢谢每一个人,我有点困惑

The [] in regex mean character class which match any one of the character listed inside it. 正则表达式中的[]表示与其中列出的任一字符匹配的字符类。

Your regex is equivalent to: 您的正则表达式等效于:

$b=~ /[program]/

which returns true as character p is found in $b . 当在$b找到字符p ,返回true。

To see if the match happens or not you are printing true , printing true will not show anything. 要查看是否发生匹配,您在打印true ,则打印true不会显示任何内容。 Try printing something else. 尝试打印其他内容。

But if you wanted to see if one string is present inside another you have to drop the [..] as: 但是,如果要查看另一个字符串中是否存在一个字符串,则必须将[..]删除为:

if ($b=~ /$a/) { print true';}

If variable $a contained any regex metacharacter then the above matching will fail to fix that place the regex between \\Q and \\E so that any metacharacters in the regex will be escaped: 如果变量$a包含任何正则表达式元字符,则上述匹配将无法解决将该正则表达式放在\\Q\\E之间的\\Q ,从而使正则表达式中的任何元字符都可以转义:

if ($b=~ /\Q$a\E/) { print true';}

Assuming either variable may come from external input, please quote the variables inside the regex: 假设任何一个变量可能来自外部输入,请在正则表达式中引用这些变量:

if ($b=~ /\Q$a\E/){print true;}

You then won't get burned when the pattern you'll be looking for will contain "reserved characters" like any of -[]{}() . 这样,当您要查找的模式包含“保留字符”(如-[]{}()任何一个)时,您就不会被烧死。

(apart the missing semicolons:) Why do you put $a in square brackets? (除了缺少的分号:)为什么将$a放在方括号中? This makes it a list of possible characters. 这使其成为可能字符的列表。 Try: 尝试:

$b =~ /\Q${a}\E/

Update 更新资料

To answer your remarks regarding = and =~ : 回答您对==~

  • =~ is the matching operator, and specifies the variable to which you are applying the regex ( $b ) in your example above. =~是匹配的运算符,在上面的示例中指定要向其应用正则表达式( $b )的变量。 If you omit =~ , then Perl will automatically use an implied $_ =~ . 如果省略=~ ,则Perl将自动使用隐含的$_ =~
  • The result of a regular expression is an array containing the matches. 正则表达式的结果是包含匹配项的数组。 You usually assign this so an array, such as in ($match1, $match2) = $b =~ /.../; 您通常为此分配一个数组,例如in ($match1, $match2) = $b =~ /.../; . If, on the other hand, you assign the result to a scalar, then the scalar will be assigned the number of elements in that array. 另一方面,如果将结果分配给标量,则将为标量分配该数组中元素的数量。

So if you write $b = /\\Q$a\\E/ , you'll end up with $b = $_ =~ /\\Q$a\\E/ . 因此,如果您编写$b = /\\Q$a\\E/ ,则会得到$b = $_ =~ /\\Q$a\\E/

$a='program';
$b='programming';
if ( $b =~ /\Q$a\E/) {
    print "match found\n";
}

If you're just looking for whether one string is contained within another and don't need to use any character classes, quantifiers, etc., then there's really no need to fire up the regex engine to do an exact literal match. 如果您只是在寻找一个字符串是否包含在另一个字符串中,并且不需要使用任何字符类,量词等,那么实际上就不需要启动regex引擎来进行精确的字面匹配。 Consider using index instead:#!/usr/bin/env perl 考虑改用index :#!/ usr / bin / env perl

#!/usr/bin/env perl

use strict;
use warnings;

my $target = 'program';
my $string = 'programming';
if (index($string, $target) > -1) {
    print "target is in string\n";
}

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

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