In a message dated 2/19/2005 2:15:52 P.M. Eastern Standard Time, [EMAIL PROTECTED] writes:
 
> Hello, i am new in Perl and i have a doubt that i think is very easy to
> result.
> I have this portion code:
>
> my $string = "cccabccc";
> my @array =$string =~ /ab|a/g;
>
> I execute this and array contains 1 element with value "ab".
>
> I would like to know if there is some way for array contains all the
> matches "ab" and "a".(container and contents).
alberto --  
 
i don't know if this really answers your question, but here goes...  
 
say that the task is to extract all occurrences of a pattern from a string and then extract all occurrences of a further sub-pattern (or sub-patterns) contained in the extracted strings.   i don't think this can be done with a single regex (unless code is embedded in the regex, which is possible), but there are other ways.  
 
for example: extract all words in a string that begin with a lower case character and also contain at least one sequence of three upper case characters, and also extract all the sequences of three upper case characters so contained.  
 
here's one approach (of course, there are others):  
 
---- code -------------------------------------------------------------------------
 
use warnings;
use strict;
 

my $upper = qr/[A-Z]/;     # any upper case character
my $lower = qr/[^A-Z\W]/;  # any lower case character (includes underscore)
 
# patterns to be extracted
my $secondary = qr/$upper{3}/;  # three successive uppers
my $primary   = qr/$lower\w*/;  # any word starting with a lower
 
my $test =
"kXXX YYY ZZZZ WWWWW VVVVV UUUUUUU lXXXXX pYYYYYY t T
tY ttttttt jjAAAj mXXXpYYY nAAAAAbSSSvDDDDDDD ggg gggggg";
 
# a schwartzian transform.  the steps occur in order from bottom to top.
my @matches =    # 4. return as array of anonymous array references
    map  { [ $_, /$secondary/g ] }    # 3. extract all secondaries also
    grep { /$secondary/ }    # 2. accept only those with secondary pattern
    $test =~ /$primary/g;    # 1. extract all occurrences of primary pattern
 
for my $ar_match (@matches) {    # for each anonymous array reference...
    local $" = '><';
    print "<@$ar_match> \n";    # print elements of anonymous array
    }
 

# a slightly different approach is to define the primary pattern in terms
# of the secondary pattern.  in this case, any extracted primary pattern
# occurrence is guaranteed to contain at least one secondary pattern
# occurrence and this saves a step in the extraction process, but the
# regex definition will be more tricky.
 
my $primary2 = qr/$lower\w*$secondary/;
 
@matches =    # 3. return as array of anonymous array references
    map  { [ $_, /$secondary/g ] }    # 2. extract all secondaries also
    $test =~ /$primary2/g;    # 1. extract all occurrences of primary2 pattern
 
print "\n";
for my $ar_match (@matches) {
    local $" = '><';
    print "<@$ar_match> \n";
    }
 
---- end code -------------------------------------------------------------------
 
hth - bill  
 
_______________________________________________
ActivePerl mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to