2014-03-05 51 views
0

的Learning Perl,男試圖捕捉在用戶給定的文件的4個字符的話,下面是其攜帶regex PATTEN匹配錯誤捕捉在Perl的正則表達式4個字符單詞

代碼段

while(<data>) 
{ 
    $caps_string = $_; #assigning data to variable 
    print "This is default string :\n $caps_string \n\n"; 

    $caps_string =~ tr/a-z/A-Z/; #lower to upper case 
    print "This is caps string :\n $caps_string \n\n"; 

    $caps_string =~ /\b[a-z]{4}\b/ig; #capturing 4 character words - which fails 
    print "4 digit words in string are : \n $caps_string \n\n"; 

} 
while loop

輸出:

This is default string : 
This is a text file data, coming from input.txt #correct 

This is caps string : 
THIS IS A TEXT FILE DATA, COMING FROM INPUT.TXT #correct 

4 digit words in string are : 
THIS IS A TEXT FILE DATA, COMING FROM INPUT.TXT #incorrect according to me 

預期輸出最後一行:

#exact 4 character words 
    THIS TEXT FILE DATA FROM 

正則表達式模式和我試圖測試字符串,顯示爲expected on regex101

輸出,怎麼在Perl使用時的模式是錯的,請指導!

+0

你可以試試我下面發佈的解決方案 –

+3

在你當前的解決方案中,你需要捕獲一個數組中的所有4個字符的單詞......然後將該數組作爲你的輸出...你在做什麼現在只是匹配模式的輸入..但你沒有存儲任何地方的實際匹配。 –

回答

2

你需要用()指定正則表達式捕獲:

$caps_string =~ /\b([a-z]{4})\b/ig; # Note the case-insensitive matching with /i 

然後你最想存儲的匹配,以及:

my @fours = $caps_string =~ /\b([a-z]{4})\b/ig; # 'THIS', 'TEXT', 'FILE', ... 

print "@fours"; # "THIS TEXT FILE DATA FROM" 

+0

不會'$ caps_string =〜/ \ b([a-z] {4})\ b/g;'將'$ caps_string'中的單詞匹配存儲爲單個字符串,如'tr'在轉換後存儲問,因爲'tr'正在保存它,所以我在想''正則表達式'也會取代它! – NoobEditor

+0

@NoobEditor:如果你想修改字符串,請使用's ///' – Zaid

+0

我試過'$ caps_string =〜s/\ b [a-z] {4} \ b/ig /;打印「字符串中的4位數字是:\ n $ caps_string \ n \ n」; 「但我仍然得到全文...心靈再次糾正我? :) – NoobEditor

3
#!/usr/local/bin/perl 
$caps_string = 'This is a text file data, coming from input.txt'; 
print "This is default string :\n $caps_string \n\n"; 

$caps_string =~ tr/a-z/A-Z/; #lower to upper case 
print "This is caps string :\n $caps_string \n\n"; 

## You already converted string to upper case 
## So your pattern needs to match upper case letter .. so [A-Z] 
## And then you would want to store all the matches in an array 
@matches = $caps_string =~ /\b[A-Z]{4}\b/g; #capturing 4 character words 
    print "4 digit words in string are : @matches \n"; 

輸出我得到:

This is default string : 
This is a text file data, coming from input.txt 

This is caps string : 
THIS IS A TEXT FILE DATA, COMING FROM INPUT.TXT 

4 digit words in string are : THIS TEXT FILE DATA FROM