com wrote:
Yep, a core module name Text::Balanced.
Randy.
Is there a module out there that I can use to p a text line and return the pieces that are enclosed in paren's? Thanks...
Is there a module out there that I can use to p a text line and return the pieces that are enclosed in paren's?
Thanks
com wrote:
Yep, a core module name Text::Balanced.
Randy.
thanks
-----Original Message-----
From: Randy W. Sims [mailto:org]
Sent: Friday, April 02, 2004 5:24 PM
To: ewalker
Cc: org
Subject: Re: nested parens
com wrote:
Yep, a core module name Text::Balanced.
Randy.
thanks
com wrote:
You don't need a module for that just use regex's:
my text_inside_parens = $string =~ m/\((.*)\)/g;
HTH
Lee.M - JupiterHost.Net
JupiterHost.Net <net> wrote:
:
: com wrote:
: > Is there a module out there that I can use to p
: > a text line and return the pieces that are enclosed
: > in paren's?
:
: You don't need a module for that just use regex's:
:
: my text_inside_parens = $string =~ m/\((.*)\)/g;
Let's test it.
use strict;
use warnings;
use Data::Dumper 'Dumper';
foreach my $string (
'(foo) (bar)',
'(foo) (bar) (baz)',
'((foo) bar)) (baz)',
'(foo bar)', ) {
my text_inside_parens = $string =~ m/\((.*)\)/g;
print Dumper \text_inside_parens;
}
__END__
I get:
$VAR1 = [
'foo) (bar'
];
$VAR1 = [
'foo) (bar) (baz'
];
$VAR1 = [
'(foo) bar)) (baz'
];
$VAR1 = [
'foo bar'
];
.* is greedy. I suspect text_inside_parens will
never have more than one element in it.
HTH,
Charles K. Clarkson
--
Mobile Homes Specialist
254 968-8328
> Let's test it.
Right, The point was, regexes could help, I never said it wasn't greedy
or the example was 100% what he needed, just a suggestion for another
place to start looking :)
(I figured it was a *little* better than `perldoc perlre` ;p)
Thanks for taking the time to do that example.
Lee.M - JupiterHost.Net
"Charles K. Clarkson" wrote:
I see. Someone dun fergot the lazy symbol, huh?
....
good point.
....
my text_inside_parens = $string =~ m/\((.*?)\)/g;
print Dumper \text_inside_parens;
}
__END__
$VAR1 = [
'foo',
'bar'
];
$VAR1 = [
'foo',
'bar',
'baz'
];
....
Joseph
>> Let's test it.
>
>
> Right, The point was, regexes could help, I never said it wasn't greedy
> or the example was 100% what he needed, just a suggestion for another
> place to start looking :)
> (I figured it was a *little* better than `perldoc perlre` ;p)[/ref]
To illustrate further try:
perl -e 'my $str = "(hi)((bye)j(hi))(sweet)";my m = $str =~
m/\(([^(^).]*)\)/g; for(m) { print "-$_-\n"; }'
See? No module, lots of regex :) Again, maybe not exactly what they
needed (IE It misses the middle 'j')but an illustration of an idea.
And so everyone who is playing along at home:
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper 'Dumper';
foreach my $string (
'(foo) (bar)',
'(foo) (bar) (baz)',
'((foo) bar)) (baz)',
'(foo bar)', ) {
my text_inside_parens = $string =~ m/\(([^(^).]*)\)/g;
print Dumper \text_inside_parens;
}
HTH
Lee.M - JupiterHost.Net
>
>[/ref]
Bookmarks