Pez Cuckow
Pez Cuckow

Reputation: 14412

Perl Regex Search and Replace Not Matching

I am trying to use perl to search and replace some outdated MySQL commands

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

However currently the file remains exactly the same before and after implying the regex isn't matching?

I have the flags smgi turned on so .* also matches new lines taking advice from Multiline search replace with Perl

Here is a snip of the file I am trying to match

        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,

If you test the pattern at http://regexpal.com/ (or any similar regex tester) - with smi on it matches fine?

Pattern: if @ApprovalRequestID.*@NextValue=@TxnDecoratorID output


This is the perl slightly split up so you can see what's going on

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

Upvotes: 0

Views: 541

Answers (1)

mob
mob

Reputation: 118595

@name is interpolated as a named Perl array inside of regular expression patterns. Escape the @ characters:

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

Upvotes: 4

Related Questions