<?php// here's the pattern:$pattern = '/<(\w+)(\s+(\w+)\s*\=\s*(\'|")(.*?)\\4\s*)*\s*(\/>|>)/';
// a string to parse:$string = 'Hello, try clicking <a href="#paragraph">here</a><br/>and check out.<hr /><h2>title</h2><a name ="paragraph" rel= "I\'m an anchor"></a>Fine, <span title=\'highlight the "punch"\'>thanks<span>.<div class = "clear"></div><br>';
// let's get the occurrences:preg_match_all($pattern, $string, $matches, PREG_PATTERN_ORDER);
// print the result:print_r($matches[0]);?>
(?=<ul\s+id="matchMe"\s+type="square"\s*>) # match start with <ul id="matchMe"...(?> # atomic group / don't backtrack (faster)<!-- .*? --> | # match xml / html comment<[^>]*/> | # self closing tag(?<opentag><(?!/)[^>]*[^/]>) | # push opening xml tag(?<-opentag></[^>]*[^/]>) | # pop closing xml tag[^<>]* # something between tags)* # match as many xml tags as possible(?(opentag)(?!)) # ensure no 'opentag' groups are on stack