Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

replace or substitute.... 2

Status
Not open for further replies.

eugene42

Programmer
Sep 25, 2011
6
US
Hi,

I'm trying to replace the following pattern with a blank field, but haven't been able to do so

$match ='C:\data\random\unnecessary';
$sentence = 'C:\data\random\unnecessary\necessary_part\this too\finally_here.docx';
$match =~ s!$sentence!!g;
print $match;

So, I'm looking for a result like,

match ====> necessary_part\this too

and also,

new_variable ====> finally_here.docx

I just wanted to know if there was a simple way to go about this, coz i would have been able to do the same in tcl using regexp match and regsub.
 
Hi

You are almost there. Just reversed the variables :
Code:
[gray]   _____________[/gray]
[gray]  v             |[/gray]
$match =~ s!$sentence!!g;
[gray]  |_____________^[/gray]
To deprive special characters in $match from their special meanings, add [tt]\Q[/tt]..[tt]\E[/tt] around the variable :
Code:
$sentence =~ s![highlight]\Q[/highlight]$match[highlight]\E[/highlight]!!g;
Regarding the second part of your question, use the File::Basename module if you want to avoid anther regular expression.


Feherke.
 
Thanks Feherke!! that worked like a gem!! Also, thanks for your hint on the tcl forum for the abstract tcl problem I had.
 
On a side note, I would advise you to remove the 'g' modifier, as global search and replace does not make sense in this situation.

Also, if you really want to be hardcore, add a boundary condition since you want this to be removed from the front of the line only.

Code:
$sentence =~ s/^\Q$match\E//;

- Miller
 
Thanks Miller, for the update.

I kinda just noticed this. The match variable still contains the filename 'finally_here.docx'.

Is there any quick way to filter that out right away in the regex, or should I go ahead and store the filename in a variable using File::Basename, and then replacing it in $sentence with a blank field. (I'll need to do this for a big chunk of files, so I'm assuming a quick fix in the above regex would be preferable.)

 
Hi

eugene42 said:
The match variable still contains the filename 'finally_here.docx'.
Oops. Missed to mention that detail : the [tt]s///[/tt] substitution will alter the value of $sentence, not $match. If you want to keep $sentence's original value, use another variable, like this :
Perl:
[COLOR=#990000]([/color][COLOR=#009900]$result[/color] [COLOR=#990000]=[/color] [COLOR=#009900]$sentence[/color][COLOR=#990000])[/color] [COLOR=#990000]=~[/color] [b][COLOR=#0000FF]s[/color][/b][COLOR=#FF6600]/^\Q$match\E//[/color][COLOR=#990000];[/color]
To do it all in a single step I would use this :
Perl:
[COLOR=#990000]([/color][COLOR=#009900]$path[/color][COLOR=#990000],[/color] [COLOR=#009900]$file[/color][COLOR=#990000])[/color] [COLOR=#990000]=[/color] [COLOR=#009900]$sentence[/color] [COLOR=#990000]=~[/color] [b][COLOR=#0000FF]m[/color][/b][COLOR=#FF6600]/^\Q$match\E(.+)\\(.+)$/[/color][COLOR=#990000];[/color]
But not that regular expressions are slower then "traditional" string manipulations, so for many files you may still prefer [tt]File::Basename[/tt].


Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top