Greetings Monks,

My regex won't let me do what I want. My goal is to build a compression on a string of digits. I only want to transform patterns of exactly 4 times a 3. I want it to count as many as matches as possible,
count the amount of occurence and for example change it to [ 4 ] or [ 8 ] or [ 12 ] and so on. This must sound very confusing, my example should clear things up. So for example, i have a var I need to transform:

my $toTranslate = "3333333333223333322";

I would like the output to become:
[ 8 ] 3322[ 4 ] 322

The script I made so far looks like this:

#!/usr/str/perl -w use strict; use warnings; my $toTranslate ="3333333333333333333333333333333333333333333333333333 +333333333333333333333333333333333333333333333333333333333333333332333 +333333223333333332333333333333333333333333333333333233333333223333333 +32333333333"; my $tmpFound; my $count = 0; while($toTranslate =~ /(3{4,})+/g) { $tmpFound = $1; $tmpFound = length($tmpFound); $toTranslate =~ s/$1/[$tmpFound]/; } print $toTranslate,"\n";


Output: [ 117 ] 2 [ 9 ] 22 [ 9 ] 2 [ 33 ] 2 [ 8 ] 22 [ 8 ] 2 [ 9 ]

The problem here though is the {4,} being ATLEAST 4 times, so it replaces "3333322" to "[ 5 ] 22".

When I make it: $toTranslate =~ /(3{4})+/g)
I face the next problem, it will store the data on exact matches, and the output will look like [ 4 ] [ 4 ] [ 4 ] 33222[ 4 ] [ 4 ] for example.

Do any of you Monks have a clue how to adjust this regex so that it will give me an output like " [ 80 ] 322[ 36 ] 23323332" etc.?

P.S. I want this done by a regex and not for example a function that calculates the output.

Kind regards,
Joris

In reply to Regex exact pattern match problem! by Jts

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.