#!/perl/bin/perl # use strict; use warnings; use diagnostics; $_ = qq(rated=0 lang="English" Channels: 0 1 250 interface="Version: 2 +.34.59.en"); print 'rated=',getvalue('rated',$_),"\n"; print 'lang=',getvalue('lang',$_),"\n"; print 'Channels: ',getvalue('Channels',$_),"\n"; print 'interface=',getvalue('interface',$_),"\n"; sub getvalue { my $s = shift; local $_ = shift; my ($value1,$value2,$value3) = / $s(?:=|:) # key word followed by either a '=' or ':' cha +racter (?(?=\d) # first case, '=' followed by a number (\d+) # gather the digits into $1 | # or (?(?=\") # second case, '=' followed by a quote \"([^\"]+)\" # gather the string without the quotes into $2 | # or \s(.*?)\s\D # default case, space followed by # anything until space and non-digit into $3 ) # end second conditional ) # end first conditional /x; if (defined($value1)) { $value1; } elsif (defined($value2)) { $value2; } elsif (defined($value3)) { $value3; } else { 'No match found'; } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Conditional Regex
by Anonymous Monk on Jan 09, 2004 at 07:14 UTC | |
by hsmyers (Canon) on Jan 09, 2004 at 15:26 UTC |