Jean
Jean

Reputation: 22705

Extract regexp matches from a string

I was trying to capture matches in a string using this:

$String = "Feb122013Jan222013";    
my @Matches = ($String =~ /((Jan|Feb)\d\d\d\d\d\d)/g);    
foreach (@Matches)
{
    printf("[$_]\n");
}

This would print the following.

[Feb122013]
[Feb]
[Jan222013]
[Jan]

How can I get rid of Jan and Feb printed ? i.e Assign only the entire regex match to array elements and ignore sub-matches ?

Upvotes: 1

Views: 166

Answers (1)

Jon Purdy
Jon Purdy

Reputation: 54999

Use (?:Jan|Feb) instead of (Jan|Feb). See (?:pattern) in perldoc perlre:

This is for clustering, not capturing; it groups subexpressions like (), but doesn’t make backreferences as () does.

Upvotes: 3

Related Questions