Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

How to remove middle of string?

by Anonymous Monk
on Jun 06, 2002 at 13:57 UTC ( [id://172192]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello all, My objective is to remove the following code from the two strings below:
set SRVBUILD=\\srvbuild\e want to remove: \\srvbuild\e set INCLUDE=%SRVBUILD%\ADW61\VENDOR\MICROSOFT\PLATFORMSDK\INCLUDE;\%SR +VBUILD%\ADW61\VENDOR\MICROSOFT\PLATFORMSDK\WTL\INCLUDE;%SRVBUILD%\KEY +BASE\SRC32\H;%MSVCDir%\ATL\INCLUDE;%MSVCDir%\INCLUDE; want to remove: everything from %SRVBUILD% to the first %MSVCDir% not +including %MSVCDir% this is the code I have so far, but no luck: foreach my $line (@vcvars) { if ($line =~ /set SRVBUILD=/) { $line =~ s/^\=(\W*)/\=INI_CODE_HERE/; print VCVARS32 $line; next; } if ($line =~ /set INCLUDE=/) { $line =~ s/set INCLUDE=/TEST/; my $result =~ s/^\%SRVBuild(\w+)^\%MSVCDir\%/INI_CODE_HERE/; print "New Line: $result\n"; print VCVARS32 $line; next; } else { print VCVARS32 $line; } }
any ideas?

Replies are listed 'Best First'.
Re: How to remove middle of string?
by Aristotle (Chancellor) on Jun 06, 2002 at 15:14 UTC
    See comments.
    foreach my $line (@vcvars) { if ($line =~ /^set INCLUDE=(.*)/) { ## use the ^ and $ anchors whe +never possible my @paths = split(/;/, $1); # $1 is the part in brackets in th +e regex above print VCVARS32 "set INCLUDE="; print VCVARS32 join(";", # concatenate list of strings using +semicolons grep( !/^\%SRVBUILD\%/, @paths) # return list of the strin +gs that do not start with %SRVBUILD% ); print VCVARS32 "\n"; next; } # no else{} necessary; next() will prevent you falling through to +here # in you "set SRVBUILD" case: # $line =~ s/^\=(\W*)/\=/; # this will not work since you're using an ^ anchor; # the "=" is not at the beginning of the string, is it? # also, \W matches any NONword-character; anything NOT a letter, d +igit or underscore # but if I understood correctly you want to change everything afte +r the "=" # you can simply use a s///ubstitution here # since if the left side does not match, nothing gets replaced; # no need for an if(){} s/^(set SRVBUILD)=.*/$1=INI_CODE_HERE/; # and then we can just let the boilerplate print handle the modifi +ed $line print VCVARS32 $line; }
    ____________
    Makeshifts last the longest.
Re: How to remove middle of string?
by atopolc (Pilgrim) on Jun 06, 2002 at 14:03 UTC
    This was covered fairly well in a node about extracting a range.

    this node

    Update: changed from an absolute link, thanks broquaint

      Thanks, but I actually am trying to replace the specified text with Different text
        The four-argument usage of perlfunc:substr will do what you are looking for. For example:

        my $string = "foo bar baz"; my $replacement = "moo"; # This will print "foo moo baz" print substr($string, 4, 3, $replacement), "\n";

         

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://172192]
Approved by Rex(Wrecks)
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (6)
As of 2024-04-23 10:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found