Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Perl (http://www.velocityreviews.com/forums/f17-perl.html)
-   -   Newbie-Reg Exp (http://www.velocityreviews.com/forums/t24753-newbie-reg-exp.html)

psk 01-16-2004 02:39 PM

Newbie-Reg Exp
 
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

Thomas Keller 01-19-2004 09:28 PM

Re: Newbie-Reg Exp
 
psk9@rediffmail.com (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 =)

Gunnar Hjalmarsson 01-19-2004 10:30 PM

Re: Newbie-Reg Exp
 
Thomas Keller wrote:
> psk9@rediffmail.com (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



All times are GMT. The time now is 04:44 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.