Jts has asked for the wisdom of the Perl Monks concerning the following question:
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:
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
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
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Regex exact pattern match problem!
by moritz (Cardinal) on Nov 20, 2008 at 16:55 UTC | |
by AnomalousMonk (Archbishop) on Nov 20, 2008 at 20:10 UTC | |
Re: Regex exact pattern match problem!
by JadeNB (Chaplain) on Nov 20, 2008 at 17:18 UTC | |
Re: Regex exact pattern match problem!
by eye (Chaplain) on Nov 20, 2008 at 19:52 UTC | |
by johngg (Canon) on Nov 20, 2008 at 23:16 UTC | |
by JadeNB (Chaplain) on Nov 21, 2008 at 00:16 UTC | |
by johngg (Canon) on Nov 21, 2008 at 13:19 UTC | |
Re: Regex exact pattern match problem!
by CountZero (Bishop) on Nov 20, 2008 at 19:40 UTC | |
by Jts (Initiate) on Nov 24, 2008 at 08:05 UTC |
Back to
Seekers of Perl Wisdom