Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Newbie-Reg Exp

Reply
Thread Tools

Newbie-Reg Exp

 
 
psk
Guest
Posts: n/a
 
      01-16-2004
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
 
Reply With Quote
 
 
 
 
Thomas Keller
Guest
Posts: n/a
 
      01-19-2004
(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 =)
 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      01-19-2004
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

 
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 needed with reg exp please Aristotle Perl 4 09-04-2004 05:38 PM
reg exp Ken Chesak Perl 5 09-01-2004 12:27 PM
reg exp Ken Chesak Perl 0 08-26-2004 03:40 PM
Reg Exp Help PerlE Perl 0 01-30-2004 06:15 AM
Reg exp: matching relative path only. Andrew Rowland Perl 0 08-01-2003 11:14 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