CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Regular Expressions: replacement with exact number of chars: Edit Log



Anglachel
Novice

Dec 3, 2010, 8:29 AM


Views: 5334
replacement with exact number of chars

Sorry for basic question. How I can replace some pattern with the exact number of characters?
Let's say I have a string AAAABBBBCCCC and variables that keep start and end positions of B (characters defined here with B in real world could be any, I just know only position of pattern I want to replace not pattern itself). And I want to replace this pattern with the same number (known for me) of X characters.
So I use

Code
my $range = $end-$start; 
$string =~ s/^(.{$start})(.{$range})(.*)$/$1?{$range}$3/;

What should I use on the place of question mark?
Now I'm using rather awkward construction:

Code
for (my $j=0; $j<$range; $j++) { 
$x .= "X"; }

(the $x plays instead of question mark in my expression)
Any help would be appreciated!


(This post was edited by Anglachel on Dec 3, 2010, 8:30 AM)


Edit Log:
Post edited by Anglachel (Novice) on Dec 3, 2010, 8:30 AM


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives