Perl,比较标量与数组(使用正则表达式?)
我会很简短,以免浪费您的时间;
简单的问题,我有一系列的狗,比如数百只狗
my @dogs = qw(Shepard Lab Dalmation Husky Chow Pitbull ...)
,我想将它与一只狗进行比较
my $doggy = "Shepard";
,我知道这很愚蠢,但我该怎么做呢?
# Regex match against array?
# (This doesnt even work but its how i would think of doing it)
if ($doggy =~ /@dogs/) {
print $doggy;
}
感谢您提前提供任何答案,我感谢你们帮助我解决这个可能非常愚蠢的问题。谢谢
I'll be brief so I don't waste your time;
Simple problem, i have an array of say, hundreds of dogs
my @dogs = qw(Shepard Lab Dalmation Husky Chow Pitbull ...)
and i want to compare it to a single dog
my $doggy = "Shepard";
pretty stupid i know, but how would i do that?
# Regex match against array?
# (This doesnt even work but its how i would think of doing it)
if ($doggy =~ /@dogs/) {
print $doggy;
}
Thanks for any answers in advance, I appreciate you guys helping me with what is probably a really stupid question. Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我会这样做:
I would do it like this :
您可以只使用 grep:
结果:
可以根据需要更改正则表达式,如果您只对第一个匹配的狗感兴趣,您会在
$wanted_dogs[0]
中找到它。You can just use grep:
Result:
The regex can be changed as wanted, and if you're only interested in the first matching dog, you'll find that in
$wanted_dogs[0]
.智能匹配运算符:
Smart match operator:
假设您并不真正想要正则表达式匹配,而只是想查看列表中的某个位置是否存在完全匹配,那么您有多种选择。
智能匹配是相对较新的,但最简单的可能是最快的方法。
经典方法是 grep 和 List::Util::first。如果您确实想要(例如)当
$dog
为“Shep”时匹配“Shepard”,您可以调整后两者以使用正则表达式而不是eq
。Assuming you don't really want a regular expression match and just want to see if there is an exact match somewhere in the list, then you have a number of options.
Smart match is the relatively new, but simplest and possibly fastest approach.
The classic methods are grep and List::Util::first. You can adjust the latter two to use a regex instead of
eq
if you do want to (for example) match "Shepard" when$dog
is "Shep".有一些使用较新的智能匹配运算符的技术,但我更喜欢旧的经典:
在引用名称中的任何正则表达式特殊字符后,创建狗类型的正则表达式替换。
如果需要,您也可以编译正则表达式:
它将放置在定义 $dogs_re 的行之后,并且如果您将多次使用正则表达式,则可能会提供一些性能优势。
There are techniques using the newer smart-match operator, but I prefer the old classic:
That creates a regex alternation of your dog types, after quoting any regex special characters in the names.
You could compile the regex as well if you want:
which would be placed after the line defining $dogs_re, and may offer some performance benefits if you will be using the regex many times.
我可能会选择哈希查找解决方案作为最佳答案,但有很多方法可以做到这一点。这是其中之一。
请注意,这使用我们的狗名作为正则表达式,并在列表上迭代它,而不是相反。
您可以控制匹配的严格程度:
/\Q$dog\E/i
最不严格,忽略大小写,匹配单词中的任何位置。例如“lab”将匹配“Yellow Lab”、“Labrador”等。/^\Q$dog\E$/
是最严格的,匹配大小写,匹配整个单词。I would probably choose the hash lookup solution for best answer, but there are many ways to do this. This is one of them.
Note that this uses our dog name as the regex and iterates it over the list instead of the other way around.
You can control how strict the match is:
/\Q$dog\E/i
is least strict, ignore case, match anywhere in word. E.g. "lab" will match "Yellow Lab", "Labrador" etc./^\Q$dog\E$/
is most strict, match case, match entire word.试试这个,
Try this,