wfsp has asked for the wisdom of the Perl Monks concerning the following question:

This does what I want
my $line = q{one <two>}; $line =~ / ([^<]+) (?: < (two|three) > )? /x; print $1, $2;
one two
The <two> is optional and that works too. But I need
my $line = q{one <four>};
to return one <four> in $1 and for $2 to be undef. Only capture to $2 if a pattern is matched otherwise capture the whole lot to $1.

Is this something that a look around assertion could handle? I've yet to conquer the blighters :-(

It will be used like

while ($line =~ /pattern/g){ # do something with $1 and $2 }
and a typical string
one [link|file.html] two [email|me@home.com] three [not a link]
What I have at the moment is fine but I'm not picking up three [not a link] in $1

while ( $line =~ / ( [^\[]* ) (?: \[ ( (?:link|email|pdf) [^\]]* ) \] )? /xg ) { my $txt = $1; my $link = $2; if ($link){ my ($type, $address, $txt) = split(/\|/, $link); # more stuff } }
tia

Replies are listed 'Best First'.
Re: Regex: Optional/alternative catpures (two)
by tye (Sage) on May 23, 2009 at 16:22 UTC

    Minor adjustment:

    $line =~ / ^ (.+?) (?: < (two|three) > )? $ /xs;

    Another approach:

    my $pre= $line; my $post= ''; if( $pre =~ s/ \s* < (two|three) > $ //xs ) { $post= $1; }

    - tye