Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Replacing words in VB code but not strings or comments

by AnomalousMonk (Abbot)
on Sep 27, 2013 at 20:49 UTC ( #1056073=note: print w/ replies, xml ) Need Help??


in reply to Replacing words in VB code but not strings or comments

Here's an approach that works for 5.14+. (Still working on something for earlier versions.) I would add some more test cases. (BTW: I'm not familiar with VB, so I'm just going by what you have described of the syntax.)

use warnings # FATAL => 'all' ; use strict; use Test::More # tests => ?? + 1 # Test::NoWarnings adds 1 test 'no_plan' ; use Test::NoWarnings; my $renames = { qw(this_sub that_sub var_1 var_a var_2 var_b and FILED), map { $_ => 'FAILED' } qw(Ignore nothing can t do it), }; my ($rx_rename) = map qr{ \b (?: $_) \b }xms, join q{ | }, keys %$renames ; # functions under test ############################################### +###### eval q{ # uses s///r from version 5.14+ # uses possessive quantifiers from 5.10+ sub replace_5_14 { my ($string, ) = @_; my $rx_dquot = qr{ " [^"]*+ (?: "" [^"]*+)*+ " }xms; my $rx_ceol = qr{ ' [^\n]*+ \n? \z }xms; my $rx_skipem = qr{ $rx_dquot | $rx_ceol }xms; return $string =~ s{ $rx_skipem*+ \K ((?: (?! $rx_skipem) .)*+) } { $1 =~ s{ ($rx_rename) }{$renames->{$1}}xmsgr }xmsger; } } if $] >= 5.014; # testing ############################################################ +###### METHOD: for my $func_name (qw(replace_5_14)) { next METHOD unless exists &{$func_name}; note "\n testing function $func_name() \n\n"; *replace = do { no strict 'refs'; *{$func_name}; }; VECTOR: for my $ar_vector ( [ qq{""}, qq{""}, ], [ qq{"'"}, qq{"'"}, ], [ qq{""""}, qq{""""}, ], [ qq{ xxx foo("var_1", "Ignore ""can't""", """") ' do it \n}, qq{ xxx foo("var_1", "Ignore ""can't""", """") ' do it \n}, ], [ qq{ this_sub("first", var_1, "Ignore ""can't""", """") ' do +it \n}, qq{ that_sub("first", var_a, "Ignore ""can't""", """") ' do +it \n}, ], [ qq{xxx can't Ignore this_sub("first", var_1, "Ignore ""can't +""", """") ' do it \n}, qq{xxx FAILED't Ignore this_sub("first", var_1, "Ignore ""ca +n't""", """") ' do it \n}, ], [ qq{xxx will Ignore't this_sub("first", var_1, "Ignore ""can' +t""", """") ' do it \n}, qq{xxx will FAILED't this_sub("first", var_1, "Ignore ""can' +t""", """") ' do it \n}, ], ) { my ($string, $expected) = @$ar_vector; is replace($string), $expected, # qq{} ; } # end for VECTOR } # end for METHOD

Output:

c:\@Work\Perl\monks\Anonymous Monk\1056009>perl parse_and_sub_vb_2.pl # # testing function replace_5_14() # ok 1 ok 2 ok 3 ok 4 ok 5 ok 6 ok 7 ok 8 - no warnings 1..8


Comment on Re: Replacing words in VB code but not strings or comments
Select or Download Code
Re^2: Replacing words in VB code but not strings or comments
by Anonymous Monk on Sep 27, 2013 at 21:56 UTC
    That is the kind of solution I was looking for. Unfortunately I am targeting 5.8.8, the version installed by the vendor of the I am supporting. Thank you for you effort.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1056073]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (5)
As of 2014-12-21 19:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (107 votes), past polls