Newbie-Reg Exp

P

psk

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
 
T

Thomas Keller

(e-mail address removed) (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 =)
 
G

Gunnar Hjalmarsson

Thomas said:
(e-mail address removed) (psk) wrote

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;
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top