The following isn't efficient because it keeps re-matching the entire string, but this won't matter unless your strings are very long. It is simple.

#! /usr/local/bin/perl use strict; use warnings; my $str = "word1, word2, #18abcdefgh ,word4, #24qwer, word5"; print "Before: $str\n"; while($str =~ m/(.*?)#(\d)(\d)(.*)/) { $str = $1 . "<Binary block($2): $3 bytes>" . substr($4, $3); } print "After: $str\n";

Which produces:

Before: word1, word2, #18abcdefgh ,word4, #24qwer, word5 After: word1, word2, <Binary block(1): 8 bytes> ,word4, <Binary block +(2): 4 bytes>, word5

I wondered about the first digit after the '#' so I captured that and put it into the substitution also.

update: removed unnecessary capture from the RE.

update2: A better alternative and comparison:

#! /usr/local/bin/perl use strict; use warnings; use Benchmark qw(cmpthese); my $str = "word1, word2, #18abcdefgh ,word4, #24qwer, word5"; print "Before: $str\n"; while($str =~ m/#(\d)(\d)/g) { substr($str, pos($str) - 3, $2 + 3, "<Binary block($1): $2 bytes>" +); } print "After: $str\n"; my $start = "word1, word2, #18abcdefgh ,word4, #24qwer, word5"; print "\n\n"; cmpthese( -10, { 're-match whole string' => sub { my $str = $start; while($str =~ m/(.*?)#(\d)(\d)(.*)/) { $str = $1 . "<Binary block($2): $3 bytes>" . substr($4 +, $3); } }, 'match #\d\d' => sub { my $str = $start; while($str =~ m/#(\d)(\d)/g) { substr($str, pos($str) - 3, $2 + 3, "<Binary block($1) +: $2 bytes>"); } }, }, );
Before: word1, word2, #18abcdefgh ,word4, #24qwer, word5 After: word1, word2, <Binary block(1): 8 bytes> ,word4, <Binary block +(2): 4 bytes>, word5 Rate re-match whole string match # +\d\d re-match whole string 75230/s -- +-28% match #\d\d 104315/s 39% + --

In reply to Re: Can I use backreferences as quantifiers in a regex? by ig
in thread Can I use backreferences as quantifiers in a regex? by johnbo

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.