troubleshooting Question

Perl Regular Expression To replace value in text of variable?

Avatar of KGNickl
KGNicklFlag for United States of America asked on
PerlRegular Expressions
3 Comments1 Solution319 ViewsLast Modified:
See the code below. Basically have a file name, a search value, and a replace value. Everything works, but I don't know regular expressions and how to write one where I have the questions marks that will replace take $file and replace the $search_value within it (if found) with the $replace_value.

Not sure if this can be done in one line w/ a regular expression or if there is another easier way? Thanks! Let me know if anything needs clarification.
my $file = "Test_12354a-bc.doc";
    my $search_value = "12354a";
    my $replace_value = "replaced";
        if($search_value =~ /\b$file\b/){
            #True, replace $search_value found in $file with the $replace_value
            $file = ?????;
            #$file should now be equal to "Test_replaced-bc.doc"
            #False, do nothing

Open in new window


Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 3 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 3 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros