pat_mc has asked for the wisdom of the Perl Monks concerning the following question:
Dear Monks,
I want to use a regex to extract ALL character n-tuplets from a given input string irrespective of case. To do so, I have written the following little script:
For $n = 2, the script provides the correct output:
Any suggestions for improvement?
Thanks for your help in advance and best regards -
Pat
I want to use a regex to extract ALL character n-tuplets from a given input string irrespective of case. To do so, I have written the following little script:
#! /usr/bin/perl -w use strict; my $a = "Input String: aaAabBAAa"; my $n = shift @ARGV; $n --; while ( $a =~ /(.)\1{$n}/ig ) { print "$&\n"; pos( $a ) = pos( $a ) - length( $& ) +1; }
For $n = 2, the script provides the correct output:
I am wondering if there is a more elegant way to do this, e.g. by setting a suitable modifier on the regex. The manual adjustment of the match position after each regex match strikes me as cumbersome.aa aA Aa bB AA Aa
Any suggestions for improvement?
Thanks for your help in advance and best regards -
Pat
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Is there an elegant way of resetting the match position?
by BrowserUk (Patriarch) on Oct 22, 2009 at 18:36 UTC | |
by LanX (Saint) on Oct 22, 2009 at 21:57 UTC | |
Re: Is there an elegant way of resetting the match position?
by bv (Friar) on Oct 22, 2009 at 17:04 UTC | |
Re: Is there an elegant way of resetting the match position?
by LanX (Saint) on Oct 22, 2009 at 17:46 UTC | |
Re: Is there an elegant way of resetting the match position?
by gmargo (Hermit) on Oct 22, 2009 at 18:17 UTC | |
Re: Is there an elegant way of resetting the match position?
by GrandFather (Saint) on Oct 22, 2009 at 20:02 UTC | |
Re: Is there an elegant way of resetting the match position?
by JadeNB (Chaplain) on Oct 22, 2009 at 21:45 UTC |
Back to
Seekers of Perl Wisdom