You are here: Re: [PHP] Regex for balanced brackets? « PHP « IT news, forums, messages
Re: [PHP] Regex for balanced brackets?

Posted by David Tulloh on 11/22/05 04:15

I think you are doing it wrong, though reading other people's regex
easily is a skill I lack.
It is however very possible.

The php manual has a section on recursive regex where it explains how to
solve the bracket problem.
http://php.net/manual/en/reference.pcre.pattern.syntax.php#regexp.reference.recursive
I think of recursive regex as creating goto loops, with all the power
and problems that come with it.

You will also have to enable the multiline flag for your test data.


David

Jeffrey Sambells wrote:

> I came across this method of matching brackets with regex in .NET
>
> http://puzzleware.net/blogs/archive/2005/08/13/22.aspx
>
> but I am wondering if it is possible to do the same in PHP?
>
> I've tried it a bit but I can't seem to get it to work properly. I'm
> just wondering if I am doing something wrong or if it is just not
> possible.
>
> Thanks.
>
> here is the code I was playing with:
>
> <?php
>
> $pattern = <<<PATTERN
> \{
> (?>
> [^{}]+
> |
> \{ (?P<DEPTH>)
> |
> \} (?P<-DEPTH>)
> )*
> (?(DEPTH)(?!))
>
> \}
> PATTERN;
>
> $subject = <<<SUBJECT
> test {
> test2 {
> test3
> }
> }
>
> test4
> {
>
> }
> SUBJECT;
>
>
> preg_match_all("/$pattern/ix",$subject,$matches);
>
> header("content-type: text/plain; ");
>
> var_dump($matches);
>
> ?>
>
>
> - Jeff
>
>

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация