为什么我会收到语法错误?
% 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.
答案 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 ; }'
否则它认为正则表达式已经结束。