regex search and replace

Discussion in 'Perl Misc' started by Joe Christl, Jul 30, 2005.

  1. Joe Christl

    Joe Christl Guest

    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, Jul 30, 2005
    #1
    1. Advertising

  2. Joe Christl

    mlj Guest

    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;
    mlj, Jul 30, 2005
    #2
    1. Advertising

  3. 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
    Gunnar Hjalmarsson, Jul 30, 2005
    #3
  4. 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
    Tad McClellan, Jul 31, 2005
    #4
  5. 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
    Gunnar Hjalmarsson, Jul 31, 2005
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mark McKay
    Replies:
    3
    Views:
    1,300
    Thomas Weidenfeller
    Jan 21, 2004
  2. Martin Evans

    RegEx conditional search and replace

    Martin Evans, Jul 5, 2006, in forum: Python
    Replies:
    6
    Views:
    549
    Blair P. Houghton
    Jul 6, 2006
  3. Gilles Ganault

    [Regex] Search and replace?

    Gilles Ganault, Nov 13, 2008, in forum: Python
    Replies:
    1
    Views:
    244
    Sion Arrowsmith
    Nov 13, 2008
  4. Schif Schaf
    Replies:
    12
    Views:
    1,248
    Anthra Norell
    Feb 8, 2010
  5. Jignesh Sutar
    Replies:
    0
    Views:
    59
    Jignesh Sutar
    Mar 18, 2014
Loading...

Share This Page