【问题标题】:Perl Regex Search and Replace Not MatchingPerl 正则表达式搜索和替换不匹配
【发布时间】:2012-08-30 07:44:51
【问题描述】:

我正在尝试使用 perl 来搜索和替换一些过时的 MySQL 命令

perl -i -pe 'BEGIN{undef $/;} s/if @RequestID.*@NextValue=@TableNameID output/if @RequestID is not NULL\n                begin\n                        select @TableNameID = @TableNameID + 1/smgi' $file1

但目前该文件在暗示正则表达式不匹配之前和之后保持完全相同?

我打开了smgi 的标志,所以.* 也匹配从Multiline search replace with Perl 获取建议的新行

这是我要匹配的文件的片段

        if ( @@rowcount = 0 )
                return 1

        update Sequence set CurrentValue = @TableNameID where Code = 'TableName'
end

/* TableNames - Approval Request ID */
if @RequestID is not NULL
begin
        exec spGetNextSequence @Name='TableName', @NextValue=@TableNameID output

        insert into TableName        /* RequestID */
        (
                TableNameID,
                TxnVrsnID,
                FldNmCod,

如果您在 http://regexpal.com/(或任何类似的正则表达式测试器)上测试模式 - 上面有 smi 匹配正常吗?

模式:if @ApprovalRequestID.*@NextValue=@TxnDecoratorID output


这是稍微拆分的 perl,所以你可以看到发生了什么

perl -i -pe 'BEGIN{undef $/;} s/
if @RequestID.*@NextValue=@TableNameID output/
if @RequestID is not NULL\n
                begin\n
                        select @TableNameID = @TableNameID + 1
/smgi' $file1

【问题讨论】:

  • 这就是为什么你使用-w,即使是单行。
  • -w 有什么作用?编辑:它会产生警告
  • 确实如此。在这种情况下,它会立即告诉您问题:Possible unintended interpolation of @RequestID in string at -e line 1.

标签: regex perl search


【解决方案1】:

@name 被插入为正则表达式模式中的命名 Perl 数组。转义@ 字符:

perl -i -pe 'BEGIN{undef $/;} s/
if \@RequestID.*\@NextValue=\@TableNameID output/
if \@RequestID is not NULL\n
                begin\n
                        select \@TableNameID = \@TableNameID + 1
/smgi' $file1

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-14
    • 1970-01-01
    • 1970-01-01
    • 2012-01-17
    • 2016-11-25
    • 1970-01-01
    相关资源
    最近更新 更多