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

后端 未结 2 1803
-上瘾入骨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."
    0 讨论(0)
  • 2021-01-21 19:41

    The \K ignores part of the match so it doesn't show up in $& (or the part of the string that s/// will replace. But, you output $_, the default variable that print uses when you give it no argument.

    I don't know why you want to return the last colon. If you know that you are only going to output the colon, you don't need fancy regex features. Match the line then output the known string:

     print ':' if /.+\@.+:/
    

    But I suspect you are actually trying to do something else. Are you trying to skip the login banner and capture whatever is after the first command-line prompt?


    Also, you one-liner looks suspicious. You have:

    cat test.txt | perl -ne 'print if /.+@.+\K:/' cat@sampleserver2:
    

    That cat@sampleserver2: is a command-line argument and the implicit while(<>){...} from the -n switch will try to open a file with that name and read it. Maybe you have a file named that. That filename isn't the target for the match operator, but I suspect you think it is.

    But, if you have a file with that name, why are you also piping another file? You don't need the cat there. This will read through lines of test.txt:

    % perl -ne 'print if /.+@.+\K:/' test.txt
    
    0 讨论(0)
提交回复
热议问题