相关疑难解决方法(0)

perl缓存正则表达式生成?

假设我有一个动态生成正则表达式然后匹配它们的函数.

例如,在下面的函数match_here一个\G锚被插入在正则表达式的开始.这简化了API,因为调用者不需要记住pos在模式中包含锚点.

#!/usr/bin/env perl
use strict;
use warnings;
use Carp;
use Data::Dumper;

sub match_here {
  my ($str, $index, $rg) = @_;
  pos($str) = $index;
  croak "index ($index) out of bounds" unless pos($str) == $index;
  my $out;
  if ($str =~ /\G$rg/) {
    $out = $+[0]; 
  }
  return $out;
}

# no match starting at position 0
# prints '$VAR1 = undef;'    
print Dumper(match_here("abc", 0, "b+"));
# match from 1 to 2
# prints '$VAR1 = …
Run Code Online (Sandbox Code Playgroud)

perl

6
推荐指数
1
解决办法
166
查看次数

标签 统计

perl ×1