Newbie-Reg Exp

Discussion in 'Perl' started by psk, Jan 16, 2004.

  1. psk

    psk Guest

    I want to replace a chunk of text with an equal number of characters.for example,

    I want to change all the characters after 11 A's (including these A's) to X's.

    somethinglike AAAAAAAAAAA.* should be changed to XXXXXXXXXX....

    So for example in a string like

    jkdshdsdhsdAAAAAAAAAAAsdsd should be converted to jkdshdsdhsdXXXXXXXXXXXXXXX
    11 A's+4 characters 14 X's---->

    Thnx,
    sk
    psk, Jan 16, 2004
    #1
    1. Advertising

  2. (psk) wrote
    > I want to change all the characters after 11 A's (including these A's) to X's.
    > somethinglike AAAAAAAAAAA.* should be changed to XXXXXXXXXX....


    At first:

    $ perldoc -q regex

    Secondly:

    The replacement of the A's isn't the problem, but I'm not sure how to
    acomplish the task of converting _somestuff also into X's, IMHO its
    not possible with regex alone... any guru out there may correct me...

    $string = "lalalaAAAAAAAAAAA_somestuff";
    $string =~ s/(.*?)A{11}(.*)/\\1XXXXXXXXXXX/;

    Tommy.

    PS: I'm new to perl, coming from php, so all compile errors are by
    design =)
    Thomas Keller, Jan 19, 2004
    #2
    1. Advertising

  3. Thomas Keller wrote:
    > (psk) wrote
    >> I want to change all the characters after 11 A's (including these
    >> A's) to X's. somethinglike AAAAAAAAAAA.* should be changed to
    >> XXXXXXXXXX....

    >
    > At first:
    >
    > $ perldoc -q regex


    Yes, but OP should better also study the s/// operator in

    perldoc perlop

    > Secondly:
    >
    > The replacement of the A's isn't the problem, but I'm not sure how
    > to acomplish the task of converting _somestuff also into X's, IMHO
    > its not possible with regex alone... any guru out there may correct
    > me...


    You don't replace anything with a regex alone. That's why you need the
    s/// operator (which is more than a regex).

    This should do it:

    s/A{11}(.*)/'X' x (11 + length $1)/e;

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Jan 19, 2004
    #3
    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. Andrew Rowland

    Reg exp: matching relative path only.

    Andrew Rowland, Aug 2, 2003, in forum: Perl
    Replies:
    0
    Views:
    1,135
    Andrew Rowland
    Aug 2, 2003
  2. PerlE

    Reg Exp Help

    PerlE, Jan 30, 2004, in forum: Perl
    Replies:
    0
    Views:
    496
    PerlE
    Jan 30, 2004
  3. Ken Chesak

    reg exp

    Ken Chesak, Aug 26, 2004, in forum: Perl
    Replies:
    0
    Views:
    617
    Ken Chesak
    Aug 26, 2004
  4. Ken Chesak

    reg exp

    Ken Chesak, Aug 30, 2004, in forum: Perl
    Replies:
    5
    Views:
    640
  5. Aristotle

    Help needed with reg exp please

    Aristotle, Sep 4, 2004, in forum: Perl
    Replies:
    4
    Views:
    475
    Gunnar Hjalmarsson
    Sep 4, 2004
Loading...

Share This Page