# Regular expression - Page 2

•  Subject
• Author
• Posted on

## Re: Regular expression

wrote:

There are no outer brackets in your example. There must be one
"bracket token" in the top (or bottom, depending from where you look)
level of bracket hierarchy and there are two in your example. There
can be more of them, three, four, etc. But then you can't determine
which one is outer (or maybe you can search for top level that has
greatest depth, but again ... that's an assumption - as is assumption

Yes, sure.

Bye.

Ivan

## Re: Regular expression

[PointedEars's example:]

<snip>

[Added more context to this portion of PointedEars's response:]

There are two sets.

I don't recall the OP explicitly stating that requirement.  In any
case, even if the OP did have a solution like yours in mind, there's
nothing wrong with expanding on the problem and applying a more
useful, general solution.  Someone searching the archives may well
have had the general solution in mind, and thanks to our discussion,
they will have a better idea about the complications at hand.

It just means there are multiple supersets.  How one deals with the
situation depends on the need, and the recursive regex pattern shown
earlier allows one to deal with the situation rather nicely.

<?php
\$data = 'foo ((bar) baz) (bla)';
\$re = '@\( ((?: (?>[^()]+) | (?R) )*) \)@x';

if (preg_match_all(\$re, \$data, \$m)) {
echo sizeof(\$m[1]), " superset(s)\n\nTheir contents:\n";
print_r(\$m[1]);
}
else {
echo "No matches.\n";
}

<snip>

--
Curtis Dyer
<?\$x='<?\$x=%c%s%c;printf(\$x,39,\$x,39);?>';printf(\$x,39,\$x,39);?>

## Re: Regular expression

What I meant to say ... To have "two outer brackets" there ...
[continue my sentence]

OK. But then you or anyone can't say that the simpliest solution is
wrong just because you *assumed* some things on the problem.

What I meant to say is that you don't know are supersets legal (as
much as I don't know if they aren't).

Anyway, I think the OP has enough informations about solution and
potential problems.

Bye,

Ivan

## Re: Regular expression

Thomas 'PointedEars' Lahn schrieb:

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Why not? The Ivan S  explicitly wrote: "Maybe you don't need regular
expresion...". What I read is from the original posting is exactly what
Ivan S wrote:

Get the index of the first occurence of "(":
<http://www.php.net/manual/en/function.strpos.php

Get the index of the last occurence of ")":
<http://www.php.net/manual/en/function.strrpos.php

Get the substr between the two indices:
<http://www.php.net/manual/en/function.substr.php

> For the application of a greedy regular

Can't see:
- why this would not work
- any regular expression here

Greetings, Helmut

## Re: Regular expression

Helmut Chang wrote:

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

^^^^^^^^^^^^^^^^^^^^                            ^^^^

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

The *example* "end text" could be or could contain ")" in the real world.

I am afraid you miss the point.

I strongly suggest you improve your English before you continue here.

PointedEars
--
realism:    HTML 4.01 Strict
evangelism: XHTML 1.0 Strict
-- Bjoern Hoehrmann

## Re: Regular expression

Thomas 'PointedEars' Lahn wrote:

His English is much better than your intelligence.  And his algorithm is
quite correct.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

## Re: Regular expression

Thomas 'PointedEars' Lahn schrieb:

...
...

I see. That's indeed a point, I (might have) missed. The OP wasn't very
clear in specifiying, what he exactly means with "two outer brackets",
and my interpretation might be correct and yours might be also...

But:

And I strongly suggest not to make premature assumption of my English...

Helmut

## Re: Regular expression

wrote:

Of course it can't, that is why:
\$text = 'some text(text here(possible text)text(possible
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
text(more text)))end text';

echo substr(\$text, \$sp=3Dstrpos(\$text, '('),strrpos(\$text,')')-\$sp+1);
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
doesn't produce the correct result.

Oh wait a minute it does!!!

Does this mean that Thomas 'MudForBrains' Lahn is wrong?

Oh no, maybe the world will now end.

No, but we do have a real dick-head in our midst.

## Re: Regular expression

Captain Paralytic wrote:

Good morning, sunshine.  So does

preg_match('#\(.*\)#s', \$text, \$m); echo \$m[0];

with *exactly* *this* *text*.  But neither one does after

\$text .= '...(...)...';

So, again, after this has been discussed, and two proper solution have been
found, you still manage to miss the point: It was only an *example* text.

PointedEars
--
realism:    HTML 4.01 Strict
evangelism: XHTML 1.0 Strict
-- Bjoern Hoehrmann

## Re: Regular expression

wrote:

=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D

=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D

Yes, I know this.

Oh dear,once again your inability to read lets you down. The OP was
very specific in his requirement:
"I need a regular expression to select all the text between two outer
brackets."

Note the word "two". He didn't say that he wanted text between all
super-sets of nested brackets, just between specifically "two outer
brackets". Now the word out can have two possible meanings in this
context. It can either refer to the outer of a set of nested brackets,
or it can (as in this case) mean outer as in "nearest the outside".

If the OP actually wanted what you are thinking, he should have worded
it differently, but he didn't.