简体   繁体   English

什么是触发器操作符?

[英]What is a flip-flop operator?

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. 我最近听说过有关Perl和Ruby中带有正则表达式的触发器,但是我无法找到它们是如何工作的以及常见的用例。

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? 现在我明白了它是什么,以及它是如何工作的,我将这个问题简单地改为:什么是触发器操作符?

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. 当左操作数为真时,Perl中的触发器运算符求值为true,并且在右操作数为真之前保持求值为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

The flip flop operator will be true for all lines from start until this is the end . 对于从startthis is the 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 : 所以,如果你的数据看起来像这样,那么上面的程序只适用于行start blah end等待:

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

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/) . 如果您不希望第一个和第二个正则表达式匹配同一行,则可以使用三个点版本: 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. 在列表上下文中, ..具有完全不同的功能:它返回顺序值列表。 eg 例如

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

I'm not familiar with Ruby, but Lee Jarvis's link suggests that it works similarly. 我不熟悉Ruby,但Lee Jarvis的链接表明它的工作方式类似。

Here is a direct Ruby translation of @dan1111's demo (illustrating that Ruby stole more than the flip_flop from Perl): 这是@ dan1111演示的直接Ruby翻译(说明Ruby窃取的内容比Perl的flip_flop更多):

while DATA.gets
  if $_ =~ /start/ .. $_ =~ /end/ 
    puts "flip flop true: #{$_}"
  else
    puts "flip flop false: #{$_}"
  end
end

__END__
foo
bar
start
inside
blah
this is the end
baz

More idiomatic ruby: 更惯用的红宝石:

DATA.each do |line|
  if line =~ /start/ .. line =~ /end/ 
    puts "flip flop true: #{line}"
  else
    puts "flip flop false: #{line}"
  end
end

__END__
foo
bar
start
inside
blah
this is the end
baz

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

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