regex search and replace

J

Joe Christl

Hello,

I've got a minor problem that I haven't been able to pin down using my
editor and Search/Replace using regex.

I got my my search part down:

(^[\w\s]{9})B([\w\s]{12})

.... but my replace is a little harder. Basically, I want to find every
line that has a B in the 10th spot, and replace anything after that
with X's for the next 12 spots. I could do this:

$1BXXXXXXXXXXXX

as my replace, but actually I'd like to NOT replace spaces and zero's,
only Alphas and 1-9 digits.

Is there anyone out there that understands regex better than I that can
help me out?


Thanks,
Joe

ps (sorry if I'm posting in the wrong newsgroup)
 
M

mlj

Joe said:
... but my replace is a little harder. Basically, I want to find every
line that has a B in the 10th spot, and replace anything after that
with X's for the next 12 spots. I could do this:

$1BXXXXXXXXXXXX

as my replace, but actually I'd like to NOT replace spaces and zero's,
only Alphas and 1-9 digits.

You could do it with a regex by using the /e modifier:

sub xrepl { my $t = shift; $t =~ s/[^0 ]/X/g; $t }

my $s = "abcdefgh Bj k0l m nop";
$s =~ s/(^[\w\s]{9}B)([\w\s]{12})/$1 . xrepl($2)/e;
 
G

Gunnar Hjalmarsson

Joe said:
I've got a minor problem that I haven't been able to pin down using my
editor and Search/Replace using regex.

I got my my search part down:

(^[\w\s]{9})B([\w\s]{12})

... but my replace is a little harder. Basically, I want to find every
line that has a B in the 10th spot, and replace anything after that
with X's for the next 12 spots. I could do this:

$1BXXXXXXXXXXXX

as my replace, but actually I'd like to NOT replace spaces and zero's,
only Alphas and 1-9 digits.

Is there anyone out there that understands regex better than I that can
help me out?

The substitution does not sound like a regex problem to me.

s%^([\w\s]{9})B([\w\s]{12})%
my $r = $2;
$r =~ tr/A-Za-z1-9/X/;
"${1}B$r";
%e;

Please read about the s/// and tr/// operators in "perldoc perlop".
 
T

Tad McClellan

Joe Christl said:
I got my my search part down:

(^[\w\s]{9})B([\w\s]{12})

... but my replace is a little harder. Basically, I want to find every
line that has a B in the 10th spot, and replace anything after that
with X's for the next 12 spots. I could do this:

$1BXXXXXXXXXXXX

as my replace, but actually I'd like to NOT replace spaces and zero's,
only Alphas and 1-9 digits.

Is there anyone out there that understands regex better than I that can
help me out?


Regexes are handy so often that we tend to forget that they are
not always the Right Tool for the job.

I'd do it without using any regexes at all:

if ( substr($_, 9, 1) eq 'B') {
substr($_, 10, 12) =~ tr/a-zA-Z1-9/X/; # or: tr/ 0/X/c;
}
 
G

Gunnar Hjalmarsson

Tad said:
Joe said:
I got my my search part down:

(^[\w\s]{9})B([\w\s]{12})

Regexes are handy so often that we tend to forget that they are
not always the Right Tool for the job.

I'd do it without using any regexes at all:

if ( substr($_, 9, 1) eq 'B') {
substr($_, 10, 12) =~ tr/a-zA-Z1-9/X/; # or: tr/ 0/X/c;
}

I did something like that before posting my reply to the OP. However, it
struck me that just testing the 10th character is far from similar to
the OP's regex. And, assuming that there is a need to use that regex for
identifying the lines, why not start from there?
 

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,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top