Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6870969
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:44:54+00:00 2026-05-27T03:44:54+00:00

Have many lines of text some lines has the following pattern /^aaa(B+)(.*)/ start with

  • 0

Have many lines of text

some lines has the following pattern /^aaa(B+)(.*)/

  • start with “aaa”
  • followed by a number of “B” (1 up to 9)
  • remainder of the line

need construct a function what get:

  • the text in a scalar, and
  • the shifting parameter how to shift the number of Bs

for example:

change_ab(2,$text)  # and the function will add 2 B
change_ab(-1, $text) #the function will remove one B

EDIT: added some examples – (in the result need to have min 1B or max 9Bs) – in my source code are these conditions, but i forgot write it here (sry))

 shifting   from     result
    2       aaaB     aaaBBB
    3       aaaBB    aaaBBBBB
   -2       aaaBBBB  aaaBB
   -3       aaaBB    aaaB          #min.1
    9       aaaBBBB  aaaBBBBBBBBB  #max.9    

my solution is splitting the scalar text into lines. Not very elegant. 🙁

Exists some better/faster solution – like one big regex without the need of splitting?

Here is my code:

use 5.014;
use warnings;

my $mytext = "some text
aaaB some another text
text3 here
aaaBB some text4
another textxxx
aaaBBBBXX some text4
another textzzzz
";

say change_ab(-1,$mytext);

sub change_ab {
    my($bshift, $text) = @_;

    my $out = "";
    foreach my $line ( split(/[\r\n]/, $text) ) {
        if( $line =~ /^aaa(B+)(.*)/) {
            my $bcnt = length($1);    
            my $wantedBcnt = $bcnt + $bshift;
            $wantedBcnt = 1 if $wantedBcnt < 1;
            $wantedBcnt = 9 if $wantedBcnt > 9;
            my $wantedBstr = sprintf("aaa%s", "B" x $wantedBcnt);

            $line =~ s/^aaaB+/$wantedBstr/;
        }
        $out .= $line . "\n";
    }
    return($out);
}

the new version based on Zaid’s answer:

use 5.014;
use warnings;

my $mytext = "some text
aaaB some another text
text3 here
aaaBB some text4
another textxxx
aaaBBBBXX some text4
another textzzzz
";

say change_ab(8, $mytext);

sub change_ab {
    $_[1] =~ s{(?<=^aaa)(B+)}{ 'B' x fixshift(length($1)+$_[0]) }gem;
    return $_[1];
}

sub fixshift {
    return 9 if $_[0] > 9;
    return 1 if $_[0] < 1;
    return $_[0];
}

Ps: if someone can give a better question title – pls. change it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T03:44:55+00:00Added an answer on May 27, 2026 at 3:44 am

    Let the /e modifier do the heavy-lifting for you:

    $mytext =~ s{(?<=^aaa)([bB]+)}{ 'B' x (length($1) + $b_shift) }gem;
    

    If $b_shift is expected to vary, wrap the operation in a single sub:

    sub change_ab {
    
        my $b_shift = +shift ;   # $_[0] = b_shift,  $_[1] = text
    
                                 # After shift, $_[0] is text
    
        $_[0] =~ s{(?<=^aaa)([bB]+)}{ 'B' x (length($1) + $b_shift) }gem;
    
        return $_[0];  # Explicit return avoids scalar context interpolation
    }
    

    Usage

    my $mytext = 
    "some text
    aaaB some another text
    text3 here
    aaaBB some text4
    another textxxx
    aaaBBBBXX some text4
    another textzzzz
    ";
    
    change_ab ( -1, $mytext );  
    
    print $mytext;
    

    Output

    some text
    aaa some another text
    text3 here
    aaaB some text4
    another textxxx
    aaaBBBXX some text4
    another textzzzz
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a test file that has many lines, each line looks something like:
I have a text file which has first line as below: j0W82LBrSdUbw Basically it
I have many lines in my script that look like: ./run -f abc.txt ./run
Basic question here - I have many lines of code that look something like:
I have a file with many lines in it. One of them look like
I have a file like this (tab delimited), but many lines 1314 0 0
I have a file that is similar to this: <many lines of stuff> SUMMARY:
I've got orders, which have many line items, and line items which belong to
Noob question, apologies. I'm compiling Java in Windows Vista's command-line and have so many
First of all have a look at here, www.zedge.net/txts/4519/ this page has so many

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.