On Mon, Mar 13, 2017 at 8:40 PM, ToddAndMargo <[email protected]> wrote:
> To grab something from the middle:
>
> $ perl6 -e 'my $x="blah(good stuff 123)yuk";
> $x ~~ m |.*\((.*)\)|;
> say "$x\n\$0=<$0>";'
>
Just a further refinement - if you want only the stuff between inner parens
e.g.
my $x="blah(good stuff 123)yuk(more yuk)";
use the negated char class of the closing marker, in P5
$x =~ m |.*\(([^)]*)\)|;
The initial ".*" isn't very helpful as:
$x =~ m |\(([^)]*)\)|;
matches just as well. I try to avoid untested matches, see the earlier
msg, but:
if ( $x =~ m |.*\(([^)]*)\)| ) {
# we got one
}
else {
# no match
}
Esp. (again P5) as, if the match fails, $0 et alia will continue to hold
what ever they had from before, e.g.
my $y="blah(bad stuff abc)yuk(more yuk)";
$y =~ m |.*\(([^)]*)\)|; # $0 has "bad stuff abc
my $x="blah[good stuff 123]yuk"; # wrong brackets
$x =~ m |.*\(([^)]*)\)|;
# $0 still has "bad stuff abc"
if ( defined $0 ) {
# probably not what you wanted.
--
a
Andy Bach,
[email protected]
608 658-1890 cell
608 261-5738 wk