regexp for bracket expression

regexp for bracket expression

am 19.09.2007 12:55:24 von rembremading

Hi all!

I am using regular expressions quite a lot.
But somthing I don't know, so far, is if there exists an
regexp matching anything which has equally many opening and closing
brackets, e.g.

(.(..)..(...(...))..)

Any idea?

Sorry for cross posting

Thank You!

Re: regexp for bracket expression

am 19.09.2007 13:00:35 von Paul Lalli

On Sep 19, 6:55 am, rembremading wrote:
> Hi all!
>
> I am using regular expressions quite a lot.
> But somthing I don't know, so far, is if there exists an
> regexp matching anything which has equally many opening and closing
> brackets, e.g.
>
> (.(..)..(...(...))..)

You have asked a Frequently Asked Question.
$ perldoc -q balance
Found in /software/perl-5.8.5-0/pkg/lib/5.8.5/pod/perlfaq6.pod
Can I use Perl regular expressions to match balanced text?

> Any idea?

As the FAQ answer states, check out Regexp::Common and Text::Balanced.

> Sorry for cross posting

You did not cross post this message. It was sent only to
comp.lang.perl.misc. If you also sent it to other groups, you must
have done it via multiposting, not crossposting. Please don't do that
again. It's extremely rude.

Paul Lalli

Re: regexp for bracket expression

am 19.09.2007 13:39:42 von rembremading

Paul Lalli wrote:

> On Sep 19, 6:55 am, rembremading wrote:
>> Hi all!
>>
>> I am using regular expressions quite a lot.
>> But somthing I don't know, so far, is if there exists an
>> regexp matching anything which has equally many opening and closing
>> brackets, e.g.
>>
>> (.(..)..(...(...))..)
>
> You have asked a Frequently Asked Question.
> $ perldoc -q balance
> Found in /software/perl-5.8.5-0/pkg/lib/5.8.5/pod/perlfaq6.pod
> Can I use Perl regular expressions to match balanced text?
>
>> Any idea?
>
> As the FAQ answer states, check out Regexp::Common and Text::Balanced.
That helped me, thank you!

>
>> Sorry for cross posting
>
> You did not cross post this message. It was sent only to
> comp.lang.perl.misc. If you also sent it to other groups, you must
> have done it via multiposting, not crossposting. Please don't do that
> again. It's extremely rude.
The reason was, that I first posted the same (english) text accidentally in
the german .perl.misc group. This is probably a bad idea. I didn't do it on
purpose.

>
> Paul Lalli

Re: regexp for bracket expression

am 28.09.2007 04:51:17 von sln

On Wed, 19 Sep 2007 13:39:42 +0200, rembremading wrote:

>Paul Lalli wrote:
>
>> On Sep 19, 6:55 am, rembremading wrote:
>>> Hi all!
>>>
>>> I am using regular expressions quite a lot.
>>> But somthing I don't know, so far, is if there exists an
>>> regexp matching anything which has equally many opening and closing
>>> brackets, e.g.
>>>
>>> (.(..)..(...(...))..)
>>
>> You have asked a Frequently Asked Question.
>> $ perldoc -q balance
>> Found in /software/perl-5.8.5-0/pkg/lib/5.8.5/pod/perlfaq6.pod
>> Can I use Perl regular expressions to match balanced text?
>>
>>> Any idea?
>>
>> As the FAQ answer states, check out Regexp::Common and Text::Balanced.
>That helped me, thank you!
>
>>
>>> Sorry for cross posting
>>
>> You did not cross post this message. It was sent only to
>> comp.lang.perl.misc. If you also sent it to other groups, you must
>> have done it via multiposting, not crossposting. Please don't do that
>> again. It's extremely rude.
>The reason was, that I first posted the same (english) text accidentally in
>the german .perl.misc group. This is probably a bad idea. I didn't do it on
>purpose.
>
>>
>> Paul Lalli

(.(..)..(...(...))..) is a result of a regular expression and is dynamic?
And you wan't to match that in a regular expression stream?
From perlre.html:
(??{ code })
.... the rules to determine where the code ends are currently somewhat convoluted
(example)
$re = qr{
\(
(?:
(?> [^()]+ ) # Non-parens without backtracking
|
(??{ $re }) # Group with matching parens
)*
\)
}x;

I'm not sure what this means actually. Are you trying to match nested parenthesis data?
I haven't looked into it, but the faq referenced Regexp::Common and Text::Balanced for
common pattern matching. This kind of leads me to think you could do it yourself with
something like this:


@UC_Nstart = (
"\\x{C0}-\\x{D6}",
"\\x{D8}-\\x{F6}",
"\\x{F8}-\\x{2FF}",
"\\x{370}-\\x{37D}",
"\\x{37F}-\\x{1FFF}",
"\\x{200C}-\\x{200D}",
"\\x{2070}-\\x{218F}",
"\\x{2C00}-\\x{2FEF}",
"\\x{3001}-\\x{D7FF}",
"\\x{F900}-\\x{FDCF}",
"\\x{FDF0}-\\x{FFFD}",
"\\x{10000}-\\x{EFFFF}",
);
@UC_Nchar = (
"\\x{B7}",
"\\x{0300}-\\x{036F}",
"\\x{203F}-\\x{2040}",
);
$Nstrt = "[A-Za-z_:".join ('',@UC_Nstart)."]";
$Nchar = "[-\\w:\\.".join ('',@UC_Nchar).join ('',@UC_Nstart)."]";
$Name = "(?:$Nstrt$Nchar*?)";
#die "$Name\n";

$RxParseXP1 =
qr/(?:<(?:(?:(\/*)($Name)\s*(\/*))|(?:META(.*?))|(?:($Name)((?:\s+$Name\s*=\s*["'][^<]*['"])+)\s*(\/*))|(?:\?(.*?)\?)|(?:!(?:(?:DOCTYPE(.*?))|(?:\[CDATA\[(.*?)\]\])|(?:--(.*?[^-])--)|(?:ATTLIST(.*?))|(?:ENTITY(.*?)))))>)|(.+?)/s;
# ( <( ( 1 12 2 3 3)|( 4 4)|( 5 56( ) 6 7 7)|( 8 8 )|( !( ( 9 9)|( 0 0 )|( 1 1 )|(
2 2)|( 3 3))))>)|4 4

$RxAttr = qr/^\s+($Name)\s*=\s*("|')/;

$RxAttr_DL1 = qr/^(?:([^'&]*?)|([^']*?))'/;
$RxAttr_DL2 = qr/^(?:([^"&]*?)|([^"]*?))"/;
$RxAttr_RM = qr/[^\s\n]+/;
$RxPi = qr/^($Name)\s+(.*?)$/s;