in reply to Replace zero-width grouping?
Your two pass solution looks fine provided the string isn't any longer than the sample you supplied, but when the string gets longer, it falls down.
my $a = "17341234173412341734123417341234"; $a=~s/(.)(?=...\1)/A/g; $a=~s/(?<=A...)(.)/B/g; print $a; A7AAB2BBB7BBB2BBB7BBB2BBB7BBB2BB
diotalevi's solution works much better
my $a = "17341234173412341734123417341234"; my $n=0; $n++ while $a =~ s/(.)(...)\1/A$2B/; print $n, $a; 12 A7AAB2BBA7AAB2BBA7AAB2BBA7AAB2BB
The downside is that as the length of the string grows, so do the number of passes, and it is having to re-scan the parts of the string it has already processed, each time through. For short strings this isn't a great problem, but if the strings are longer, then there is an alternative method that avoids it.
my $a = "17341234173412341734123417341234"; substr($a, $_, 5) =~ s[(.)(...)\1][A$2B] for 0 .. length ($a); print $a; A7AAB2BBA7AAB2BBA7AAB2BBA7AAB2BB
By using substr as an lvalue for the substitution, you can perform process in a single (overlapping) pass that reduces the work done by the regex engine by only looking at each group of 5 characters at a time.
The difference in performance only really becomes evident once the string length gets above about 5 times the length of your original sample, but the technique is useful in its own right for some things.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Replace zero-width grouping?
by hv (Prior) on May 07, 2003 at 02:12 UTC | |
by Enlil (Parson) on May 07, 2003 at 08:14 UTC | |
by BrowserUk (Patriarch) on May 07, 2003 at 02:24 UTC | |
|
Re^2: Replace zero-width grouping? (s/// against substr)
by Aristotle (Chancellor) on May 07, 2003 at 12:38 UTC | |
by BrowserUk (Patriarch) on May 08, 2003 at 21:56 UTC | |
by Aristotle (Chancellor) on May 09, 2003 at 06:22 UTC | |
by BrowserUk (Patriarch) on May 09, 2003 at 07:24 UTC | |
by Aristotle (Chancellor) on May 09, 2003 at 09:55 UTC | |
| |
by hv (Prior) on May 09, 2003 at 11:36 UTC |