We help IT Professionals succeed at work.

How to make a string substitution in a very long string in Perl?

Tolgar
Tolgar asked
on
Hi,
I have a very long string and in that string I have the following pattern in most places.


href="file://///dir1/dir2/dir3/dir4/dir5/dir6/dir7/dir8/dir9/dir10/dir11/REPORT_Unix/20YYMMDD_HHMM_ReportDetails.html#/dir1/dir2/dir3/dir4/dir5/dir6/dir7/dir8/dir9/dir10/dir11/@dir12/@13/file1.cpp

Open in new window



I'd like to convert this types of patterns to the following form:
href="file://///***/20YYMMDD_HHMM_ReportDetails.html#/dir1/dir2/dir3/dir4/dir5/dir6/dir7/dir8/dir9/dir10/dir11/@dir12/@13/file1.cpp

Open in new window


How can I do it?

I tried the following but it did not work:

$fileLine =~ s{ href=\w+_20 }{\ /20};

Open in new window


The directory names (dir1, dir2, dir3) may change so the rule should be something like this?

Replace between href="file:///// and 20YYMMDD_HHMM with ***


Thanks,
Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2015
Commented:
Try:

$fileLine =~ s#href="(file://///).*?(/20YYMMDD_HHMM)#$1***$2#;

Open in new window

Author

Commented:
@kaufmed: It just replaced href="(file: with ***

Author

Commented:
Any ideas???
CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2015

Commented:
What is your environment? I was testing in Windows with ActiveState Perl, and my test was successful:

Screenshot

Author

Commented:
I tried it both on unix and windows.
ozo
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:
What exactly did you try, and how did you try it?

Author

Commented:
I apologize. It works. I made a mistake in overwriting to the file.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.