2016-01-13 22 views
-3

開始和結束邊界這是我的代碼如何設置在Perl

use strict; 
    use warnings; 
    my $string=" The UNIX grep filters the lines of a file based on a regular expression.Perl's grep can filter any list of value based on any condition. This Perl code implements a basic version of the UNIX grep: "; 
    $string =~ m/grep/; 
    print "$& $'\n"; 

這是我的輸出:

的grep可以過濾基於任何條件任何有價值的名單。 此Perl代碼實現了UNIX的grep的基本版本:

但我想輸出是

的grep可以過濾基於任何條件任何有價值的名單。 此Perl代碼

任何人都可以幫助我?

+0

'打印((分/(= grep的)/,$字符串)[1], 「\ n」?)' – Borodin

+0

感謝您的答覆,但我改變像print((split /(?= filters)/,$ string)[1],「\ n」);輸出是[根據正則表達式過濾文件的行.Perl的grep可以根據任何條件過濾任何值的列表。此Perl代碼實現UNIX grep的基本版本:]它不能設置結束邊界值。想要打印,直到[代碼]我做什麼 – Chella

+0

啊好吧。你需要'print $ string =〜/(filters.*?code)/,「\ n」' – Borodin

回答

0

試試下面的代碼:

$string =~ m/\..*?(grep.*?code)/; 
print "$1\n"; 
+0

感謝您的回覆。當我使用這段代碼時,出現以下錯誤:[在串行(。)中使用未初始化的值$ 1或在str.pl行13使用字符串。] – Chella

+0

您可以共享完整的代碼塊,以便更容易找出錯誤,因爲我提供的解決方案對我來說工作得非常好。 –

相關問題