1

我写了一个 perl 脚本,它将输出一个包含类似条目的列表,如下所示:

$var = ' whatever'

$var 包含:单引号、空格、单词whatever、单引号

实际上,这是哈希的关键,我想提取相同的值。但由于单引号和中间的空格,我无法提取哈希键值。

所以,我想剥离 $var 如下:

$var = whatever

意思是删除单引号、空格和尾随单引号。

这样我就可以使用 $var 作为哈希键来提取相应的值。

你能在 perl oneliner 上指导我吗?

谢谢。

4

3 回答 3

3

这里有几种方法可以做到这一点,但要注意 - 修改哈希中的键可能会导致不需要的结果,例如:

use strict;
use warnings;
use Data::Dumper;

my $src = {
    "a a"       => 1,
    " a a "     => 2,
    "' a a '"   => 3,
};
print "src: ", Dumper($src);
my $trg;

@$trg{ map { s/^[\s']*(.*?)[\s']*$/$1/; $_ } keys %$src } = values %$src;
print "copy: ", Dumper($trg); 

将产生:

src: $VAR1 = {
          ' a a ' => 2,
          '\' a a \'' => 3,
          'a a' => 1
        };
copy: $VAR1 = {
          'a a' => 1
        };

任何正则表达式都可以使用 YAPE::Regex::Explain 模块进行解释。(来自 CPAN)。对于上述正则表达式:

use YAPE::Regex::Explain;
print YAPE::Regex::Explain->new( qr(^[\s']*(.*?)[\s']*$) )->explain;

将产生:

正则表达式:

(?-imsx:^[\s']*(.*?)[\s']*$)

matches as follows:

NODE                     EXPLANATION
----------------------------------------------------------------------
(?-imsx:                 group, but do not capture (case-sensitive)
                         (with ^ and $ matching normally) (with . not
                         matching \n) (matching whitespace and #
                         normally):
----------------------------------------------------------------------
  ^                        the beginning of the string
----------------------------------------------------------------------
  [\s']*                   any character of: whitespace (\n, \r, \t,
                           \f, and " "), ''' (0 or more times
                           (matching the most amount possible))
----------------------------------------------------------------------
  (                        group and capture to \1:
----------------------------------------------------------------------
    .*?                      any character except \n (0 or more times
                             (matching the least amount possible))
----------------------------------------------------------------------
  )                        end of \1
----------------------------------------------------------------------
  [\s']*                   any character of: whitespace (\n, \r, \t,
                           \f, and " "), ''' (0 or more times
                           (matching the most amount possible))
----------------------------------------------------------------------
  $                        before an optional \n, and the end of the
                           string
----------------------------------------------------------------------
)                        end of grouping
----------------------------------------------------------------------

简而言之:s/^[\s']*(.*?)[\s']*$/$1/;意思是:

  • 在字符串的开头尽可能多地匹配空格或撇号,
  • 然后匹配任何东西
  • 尽可能多地匹配字符串空格或撇号的末尾
  • 并保留唯一的“任何”部分
于 2012-07-20T08:37:29.980 回答
2
#!/usr/bin/perl
$string = "' my string'";
print $string . "\n";
$string =~ s/'//g;
$string =~ s/^ //g;
print $string;

输出

' my string'
my string
于 2012-07-20T07:36:39.663 回答
1
$var =~ tr/ '//d;

参见:tr 运算符

或者,通过正则表达式

$var =~ s/(?:^['\s]+)|'//g;

后者将保留单词中间的空格,前者删除所有空格和单引号。

一个简短的测试:

...
$var = q{' what ever'};
$var =~ s/
         (?:     # find the following group
           ^        # at string begin, followed by      
           ['\s]+   # space or single quote, one or more
         )       # close group
         |       # OR
         '       # single quotes in the while string 
         //gx ;  # replace by nothing, use formatted regex (x)
print "|$var|\n";
...

印刷:

|what ever|

正如预期的那样。

于 2012-07-20T07:44:10.767 回答