Home > Archive > PERL Miscellaneous > July 2005 > regex search and replace
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
regex search and replace
|
|
| Joe Christl 2005-07-30, 9:01 am |
| 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)
| |
|
| Joe Christl wrote:
> ... 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;
| |
| Gunnar Hjalmarsson 2005-07-30, 5:00 pm |
| Joe Christl wrote:
> 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".
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
| |
| Tad McClellan 2005-07-30, 10:00 pm |
| Joe Christl <jchristl@zdnetmail.com> wrote:
> 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;
}
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
| |
| Gunnar Hjalmarsson 2005-07-30, 10:00 pm |
| Tad McClellan wrote:
> Joe Christl wrote:
<snip>
[color=darkred]
> 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?
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
|
|
|
|
|