的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使用時的模式是錯的,請指導!
你可以試試我下面發佈的解決方案 –
在你當前的解決方案中,你需要捕獲一個數組中的所有4個字符的單詞......然後將該數組作爲你的輸出...你在做什麼現在只是匹配模式的輸入..但你沒有存儲任何地方的實際匹配。 –