Re: Regexp for Match Brackets
by davido (Cardinal) on May 05, 2006 at 06:47 UTC
|
I'm confused. Please explain how your needed output differs from your original string; they appear the same to me. If you intend to capture everything, please explain what you're trying to not capture, or at least what you would like to fail to match.
Update:
Do you really want Text::Balanced's extract_bracketed() subroutine? It can be used to essentially capture the "balanced parens" portion of a string. Observe:
use strict;
use warnings;
use Text::Balanced qw( extract_bracketed );
my $text='(one dfd(two(three)four()(five)df())df)';
my $balanced = extract_bracketed( $text );
print "$balanced\n";
Also note, if the return value of extract_bracketed() is undef (in scalar context), it means the parens weren't balanced.
| [reply] [Watch: Dir/Any] [d/l] [select] |
Re: Regexp for Match Brackets
by Samy_rio (Vicar) on May 05, 2006 at 07:12 UTC
|
Hi gopalr, If I understood your question correctly then try this,
use Regexp::Common;
while (<DATA>) {
(/$RE{balanced}{-begin => "("}{-end => ")"}/) ? (print "Line $. : Matc
+hed: $&\n") : (print "Line $. : Not Matched\n");
}
__DATA__
This is my (one dfd(two(three)four()(five)df())df) text.
This is my (one text.
Output is :
Line 1 : Matched: (one dfd(two(three)four()(five)df())df)
Line 2 : Not Matched
Regards, Velusamy R. eval"print uc\"\\c$_\""for split'','j)@,/6%@0%2,`e@3!-9v2)/@|6%,53!-9@2~j';
| [reply] [Watch: Dir/Any] [d/l] [select] |
Re: Regexp for Match Brackets
by blazar (Canon) on May 05, 2006 at 08:48 UTC
|
This kind of match is notoriously hard to perform. Try some dedicated module like Text::Balanced. OTOH I'm sure I saw at least one very in-depth article explaining how to do it with regexen. I can't remember where to find it. I bet japhy knows, though...
| [reply] [Watch: Dir/Any] |
|
| [reply] [Watch: Dir/Any] |
|
my $paren_rx;
$paren_rx = qr{
\(
(?:
(??{ $paren_rx }) # either match another paren-set
| (?s: [^\\()]+ | \\. )+ # or match non-parens (or escaped parens
+)
)?
\)
}x;
my ($chunk) = $string =~ /((??{$paren_rx}))/;
It's not kid-tested, nor mother-approved, but it feels right, and that's where it counts. In the gut. Long live truthiness. Stephen Colbert++.
| [reply] [Watch: Dir/Any] [d/l] |
|
| [reply] [Watch: Dir/Any] |
Re: Regexp for Match Brackets
by sen (Hermit) on May 05, 2006 at 06:50 UTC
|
| [reply] [Watch: Dir/Any] [d/l] |
|
| [reply] [Watch: Dir/Any] [d/l] |
Re: Regexp for Match Brackets
by ruzam (Curate) on May 05, 2006 at 16:28 UTC
|
If all you want to do is test for an equal number of '(' and ')' then this will do the trick:
use strict;
my $text='(one dfd(two(three)four()(five)df())df)';
print $text if (my @x = $text =~ m/\(/g) == (my @y = $text =~ m/\)/g);
This counts up the number of '(' and the number of ')' and prints $text if the counts equal.
Note: this doesn't test if the brackets are balanced! $text=')()(()' would match just as easily as $text='((()))'
Also, I don't think using $& is a good idea for this problem. | [reply] [Watch: Dir/Any] [d/l] |
Re: Regexp for Match Brackets
by gube (Parson) on May 05, 2006 at 07:15 UTC
|
#!/usr/local/bin/perl
use strict;
use warnings;
my $text='(one dfd(two(three)four()(five)df())df)';
print $& if $text=~m#(.*)#;
| [reply] [Watch: Dir/Any] [d/l] |