Nested tags

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
sachv
Forum Newbie
Posts: 1
Joined: Tue Jan 01, 2008 4:27 pm

Nested tags

Post by sachv »

Hi,

I have a regex here

Code: Select all

"#<([a-z0-9-_]+?)>(.*?)</\\1>#is"
to catch tags (my own tags, not html) like <hello>some other text</hello>, which works fine.
But I want to handle nested tags like <hello><world>some text</world></hello>, and I now need to find a way to
let it match tags that do NOT contain any other tags inside them, like the first example.
I presume I'll have to exclude it from the (.*?) group, but I don't know how to implement it.
I tried

Code: Select all

"#<([a-z0-9-_]+?)>([^(<([a-z0-9-_]+?)>(.*?)</\\1>)]*?)</\\1>#is"
but it didn't work.

Any help is appreciated,

S.
User avatar
arjan.top
Forum Contributor
Posts: 305
Joined: Sun Oct 14, 2007 4:36 am
Location: Hoče, Slovenia

Post by arjan.top »

something like this?

Code: Select all

#<([a-z0-9-_]+?)>(?!<([a-z0-9-_]+?)>)(.*?)</\\1>#is
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post by vigge89 »

http://php.net/manual/en/function.preg- ... llback.php
See Example#3 preg_replace_callback() using recursive structure to handle encapsulated BB code
Post Reply