How to use the Perl script to substitute the string ?

R

Richard Lee

Hi all,

How to use the Perl script to substitute the string as below ?

Before :-
"This is a test message on 20070715230010987654321 abcd123456"
change to
"This is a test message on 200707152300109876***** abc****456"

Please help ! Many Thanks.

Richard
 
S

shaimelz

substr EXPR,OFFSET,LENGTH,REPLACEMENT

$string = "This is a test message on 20070715230010987654321 abcd123456";

substr ($string, -7, 4, '****');
substr ($string, -18, 5, '*****');

print "$string \n";
 
R

Richard Lee

Hi Shaimelz,

Many Thanks for your help ! Sorry, my question may be not clear. In a text
file, this string may be occurred many times and I need to replace it by "*"
for masking. So I need to locate this string first and replace part of the
characters to "*".

Sample of the Text file :-
This is a test message on 20070715230010987654321 abcd123456
This is 2nd line
This is 3rd line
This is a test message on 20070715230112112233445 qazw123456
This a 5th line
This is another type : This is a test message on 20070715230112112233445
qazw123456
This a 6th line

Result of the Text file:-
This is a test message on 200707152300109876***** abc****456
This is 2nd line
This is 3rd line
This is a test message on 200707152301121122***** qaz****456
This a 5th line
This is another type : This is a test message on 200707152301121122*****
qaz****456
This a 6th line

How to use the reg expression to locate "This is a test message on 200707"
and replace some of the character follow by this criteria ? Pls help ! Many
Thanks.

Richard
 
S

shaimelz

my $textfile = <<TEXTFILE;
your text here
TEXTFILE

my @textfile = ($textfile =~ m/^\s*(.+)/gm);

foreach (@textfile) {

if ($_ =~ /(^.+)(\d{23})(\s+)(\D{4}\d{6}$)/mx) {
my ($a, $b, $c, $d) = ($1, $2, $3, $4);
substr ($b, -5, 5, '*****');
substr ($d, -7, 4, '****');
print "$a$b$c$d\n";
} else {
print "$_\n";
}

}
 
R

Randal L. Schwartz

First, comp.lang.perl has been officially dead for 12 years.
Please stop posting here.

Dshaimelz> if ($_ =~ /(^.+)(\d{23})(\s+)(\D{4}\d{6}$)/mx) {

Second, don't use "$_ =~ ...", since $_ is the default.

Third, don't use /x if it doesn't actually have an effect.

Dshaimelz> my ($a, $b, $c, $d) = ($1, $2, $3, $4);

Fourth, never use my $a or my $b, because it breaks sort subroutines.

Fifth, might as well have assigned this during the match:

if (my ($w, $x, $y, $z) = /(^.+)(\d{23})(\s+)(\D{4}\d{6}$)/m)


Sixth... you seem to be a junior Perl programmer. Please stop answering
questions. It makes it *harder* for experts to answer questions when junior
people try to help, and just make mistakes and mislead people.

Learn a bit more before answering.
 
J

Joe Smith

Richard said:
Sorry, my question may be not clear. In a text file, this string

What string? You need to be more explicit. The examples you've shown
can be interpreted different ways; what _exactly_ are you looking for?

Before asking for help, first show us the code you've already written.

If you are serious about getting help on this, repost your question
to comp.lang.perl.misc instead of here. (This newsgroup is defunct.)

-Joe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top