Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > regex search and replace

Reply
Thread Tools

regex search and replace

 
 
Joe Christl
Guest
Posts: n/a
 
      07-30-2005
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)

 
Reply With Quote
 
 
 
 
mlj
Guest
Posts: n/a
 
      07-30-2005
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;
 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      07-30-2005
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
 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      07-30-2005
Joe Christl <> 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
Perl programming
Fort Worth, Texas
 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      07-30-2005
Tad McClellan wrote:
> Joe Christl wrote:
>> I got my my search part down:
>>
>> (^[\w\s]{9})B([\w\s]{12})


<snip>

> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with regex search-and-replace (Perl to Python) Schif Schaf Python 12 02-08-2010 08:30 AM
[Regex] Search and replace? Gilles Ganault Python 1 11-13-2008 10:57 AM
How make regex that means "contains regex#1 but NOT regex#2" ?? seberino@spawar.navy.mil Python 3 07-01-2008 03:06 PM
RegEx conditional search and replace Martin Evans Python 6 07-06-2006 08:43 PM
Search and replace with NIO and Regex? Mark McKay Java 3 01-21-2004 05:29 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57