Code: Select all
/<$element(.*)>(.*)<\/$element>/isUCode: Select all
<div id="first"><div id="second"></div></div>Code: Select all
<div id="first"><div id="second"></div>Moderator: General Moderators
Code: Select all
/<$element(.*)>(.*)<\/$element>/isUCode: Select all
<div id="first"><div id="second"></div></div>Code: Select all
<div id="first"><div id="second"></div>Be very careful with those greedy DOT-STAR or DOT-PLUS monsters: before you know it, they take far more than you might think.david64 wrote:Hi, I am using the following regular expression to match HTML tags with the preg (PCRE) functions in PHP:
Code: Select all
/<$element(.*)>(.*)<\/$element>/isU
Code: Select all
"aaa { bbb } ccc { ddd } eee fff ggg"Code: Select all
"/{.*}/"Code: Select all
"{ bbb } ccc { ddd }"Code: Select all
"/{.*?}/"Code: Select all
'/{[^{}]*}/'No, not with look arounds. Using PCRE, you can capture up to a fixed number of nested tags, but not an arbitrary number of tags. You could do that like this:david64 wrote:This works fine for picking up the first part of the tag, but not always for the tag contents. For example, if you have the following HTML:
The regular expression will return the following for the first div:Code: Select all
<div id="first"><div id="second"></div></div>
Is there any way using regex that the full contents of the tag could be returned? Maybe conditions or lookaround?Code: Select all
<div id="first"><div id="second"></div>
Code: Select all
"aaa { bbb { ccc ddd } eee { fff } ggg } hhh"Code: Select all
'/{([^{}]*{[^{}]*})*[^{}]*}/'Code: Select all
"aaa { bbb { ccc { ddd } } eee fff ggg } hhh"Code: Select all
$text = 'aaa <div id="1"> bbb <div id="2"> <div id="3"> ccc </div></div> ddd </div> fff';
preg_match_all('#<div[^>]*>(?:(?:(?!</?div).)*|(?R))*</div>#si', $text, $matches);
print_r($matches);Code: Select all
<div[^>]*> // match an opening div-tag
(?: // open non-capturing group 1
(?: // open non-capturing group 2
(?!</?div). // if there's not an opening- or closing div tag when looking ahead, match any character
) // close non-capturing group 2
* // non-capturing group 2 zero or more times
| // OR
(?R) // a recursive match of this entire regex pattern
) // close non-capturing group 1
* // non-capturing group 1 zero or more times
</div> // match a closing div-tag