Bareword发现运营商预计-e第1行附近“9A”附近

时间:2013-11-27 05:08:24

标签: perl

为什么我会收到语法错误?

% perl -ne 'if (/https://([-.0-9A-Za-z]+\.[-0-9A-Za-z]+)/) { print $1 ; }'
Bareword found where operator expected at -e line 1, near "9A"
        (Missing operator before A?)
Bareword found where operator expected at -e line 1, near "9A"
        (Missing operator before A?)
syntax error at -e line 1, near "9A"
syntax error at -e line 1, near ";}"
Execution of -e aborted due to compilation errors.

2 个答案:

答案 0 :(得分:5)

如果正则表达式包含斜杠,请使用其他字符和显式m运算符:

perl -ne 'if (m%https://([-.0-9A-Za-z]+\.[-0-9A-Za-z]+)%) { print $1 ; }'

或者:

perl -ne 'print $1 if m{https://([-.0-9A-Za-z]+\.[-0-9A-Za-z]+)}'

答案 1 :(得分:3)

在https:

之后,//前面需要反斜杠
perl -ne 'if (/https:\/\/([-.0-9A-Za-z]+\.[-0-9A-Za-z]+)/) { print $1 ; }'

否则它认为正则表达式已经结束。