【问题标题】:Bareword found where operator expected at -e line 1, near "9A"Bareword 在 -e 第 1 行的“9A”附近找到操作员预期的位置
【发布时间】:2013-11-27 05:08:24
【问题描述】:

为什么会出现语法错误?

% 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.

【问题讨论】:

标签: perl


【解决方案1】:

如果正则表达式包含斜杠,请使用不同的字符和显式 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]+)}'

【讨论】:

    【解决方案2】:

    https 后面的 // 前面需要反斜杠:

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

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

    【讨论】:

    • 呃,有道理! \o\ |o| /o/ 有没有其他方法可以避免挥手? ^_^
    猜你喜欢
    • 2022-11-16
    • 2019-08-12
    • 1970-01-01
    • 2019-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-17
    相关资源
    最近更新 更多