什么是触发器运算符? [英] What is a flip-flop operator?

查看:165
本文介绍了什么是触发器运算符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我听说过Perl和Ruby中带有正则表达式的触发器,但是我找不到它们的真正作用以及常见用例.

I have heard and read about flip-flops with regular expressions in Perl and Ruby recently, but I was unable to find how they really work and what the common use cases are.

任何人都可以以与语言无关的方式对此进行解释吗?

Can anyone explain this in a language-agnostic manner?

现在,我了解它的含义以及它的工作原理,我将问题改写为:什么是触发器运算符?

Now that I understand what it is, and how it works, I would rephrase the question to be simply: What is a flip-flop operator?

推荐答案

当左操作数为true时,Perl中的触发器运算符将评估为true,直到右操作数为true为止,其评估将一直为true.左右操作数可以是任何一种表达式,但最常与正则表达式一起使用.

The flip-flop operator in Perl evaluates to true when the left operand is true, and keeps evaluating to true until the right operand is true. The left and right operand could be any kind of expression, but most often it is used with regexes.

使用正则表达式,对于找到两个标记之间的所有行很有用.这是一个简单的示例,说明其工作方式:

With regexes, it is useful for finding all the lines between two markers. Here is a simple example that shows how it works:

use Modern::Perl;

while (<DATA>)
{
    if (/start/ .. /end/)
    {
        say "flip flop true: $_";
    }
    else
    {
        say "flip flop false: $_";
    }
}

__DATA__
foo
bar
start
inside
blah
this is the end
baz

startthis is the end的所有行,触发器运算符均为true.

The flip flop operator will be true for all lines from start until this is the end.

运算符的两个点的版本允许第一和第二个正则表达式都在同一行上匹配.因此,如果您的数据看起来像这样,则上述程序仅对start blah end行有效:

The two dot version of the operator allows first and second regex to both match on the same line. So, if your data looked like this, the above program would only be true for the line start blah end:

foo
bar
start blah end
inside
blah
this is the end
baz

如果您不希望第一个和第二个正则表达式匹配同一行,则可以使用三个点的版本:if (/start/ ... /end/).

If you don't want the first and second regexes to match the same line, you can use the three dot version: if (/start/ ... /end/).

请注意不要将触发器运算符与范围运算符混淆.在列表上下文中,..具有完全不同的功能:它返回一个顺序值列表.例如

Note that care should be taken not to confuse the flip-flop operator with the range operator. In list context, .. has an entirely different function: it returns a list of sequential values. e.g.

my @integers = 1 .. 1000; #makes an array of integers from 1 to 1000. 

我不熟悉Ruby,但是李·贾维斯(Lee Jarvis)的链接暗示它的工作原理类似.

I'm not familiar with Ruby, but Lee Jarvis's link suggests that it works similarly.

这篇关于什么是触发器运算符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆