2012-07-20 43 views
1

我寫了一個perl腳本whihc將輸出包含類似下面類似的條目列表:Perl的文本處理

$var = ' whatever' 

是$ var包含:單引號,空格,字什麼,單引號

實際上,這是一個散列的關鍵,我想拉相同的值。但是由於單引號和位置之間的空間,我無法提取散列鍵值。

所以,我想要去除是$ var如下:

$var = whatever 

意味着去掉單引號,空間和後單引號。

這樣我可以使用$ var作爲散列鍵來拉取相應的值。

你能指導我的perl oneliner是否一樣。

thnaks。

回答

2
#!/usr/bin/perl 
$string = "' my string'"; 
print $string . "\n"; 
$string =~ s/'//g; 
$string =~ s/^ //g; 
print $string; 

輸出

' my string' 
my string 
+0

完美。這工作正常,我現在可以拉取哈希值。非常感謝您的寶貴時間。 – slayedbylucifer 2012-07-20 08:51:15

1
​​

見:tr operator

,或者由正則表達式

$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| 

如預期。

+0

第二個與正則表達式不起作用。我稍後會試一試。感謝您的建議。 – slayedbylucifer 2012-07-20 08:52:33

+0

@Dheeraj,根據您提供的輸入類型,它可以工作。看到我添加的解釋。這只是您在一個正則表達式中選擇的兩個正則表達式的組合。 – 2012-07-20 11:43:17

+0

現在我明白了。感謝您的解釋。你們在正則表達式中表現得太好了。 – slayedbylucifer 2012-07-20 15:48:21

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/;平均:

  • 在字符串匹配的開始空格或撇號儘可能多次,
  • 然後在字符串空格或撇號的末尾儘可能多的次數儘可能
  • 匹配匹配任何內容,並保持只有「東西」的一部分
+0

啊,他的代碼中的正則表達式太複雜了,我不明白。我會稍後嘗試解碼。謝謝。目前,cppcoder給出的解決方案正在爲我工​​作。 – slayedbylucifer 2012-07-20 08:53:48

+0

編輯爲「以後」正則表達式解釋。 :) – jm666 2012-07-20 09:05:39

+1

這真棒。我不知道** YAPE :: Regex :: Explain **。在我的perl腳本中,我使用正則表達式已經有相當長的一段時間了,有些我自己寫了自己,還有一些從谷歌複製而來,卻不知道它是如何工作的。現在我想我將能夠解碼和理解perl正則表達式。我不能夠感謝你。我真的很欣賞你在正則表達式上的解釋。 – slayedbylucifer 2012-07-20 10:21:32