Why is Perl regex \K not excluding the pattern matched before \K?

后端 未结 2 1845
-上瘾入骨i
-上瘾入骨i 2021-01-21 18:46

I have a file called test.txt with the following:

> Last login: Mon Jul 13 05:09:33 2020 You have mail.   
> ******************************
> cat, you ha         


        
2条回答
  •  庸人自扰
    2021-01-21 19:32

    You may want to say:

    perl -ne 'print $& if /.+@.+\K:/' test.txt
    

    which outputs:

    :
    
    • The statement print if CONDITION is equivalent to print $_ if CONDITION and just prints the line without modifications if the CONDITION meets. Please use $& instead.
    • The second argument cat@sampleserver2: will cause an error saying "Can't open cat@sampleserver2:: No such file or directory."

提交回复
热议问题