Grego Posted December 23, 2007 Share Posted December 23, 2007 Essentially, I want to look for the same bit of text twice in the string. So if I had the string "<tag>Hello</tag>", it would be recognised. However, if the string were "<tag>Hello</notag>" I want it to not be recognised. Currently I'm using my extremely basic regex of: "<(.*?)>(.*?)<\/(.*?)>" but this is mixing the tags up and causing problems. How can I change it so that the first and third references (in bold) have to be the same? Thanks in advance =D Quote Link to comment Share on other sites More sharing options...
dsaba Posted December 24, 2007 Share Posted December 24, 2007 give sample data and sample outputs/matches Quote Link to comment Share on other sites More sharing options...
dsaba Posted December 24, 2007 Share Posted December 24, 2007 Really need to see some sample data, but study/take a look at these patterns for matching duplicate xml nodes in xml data: ~(?m)^(.*+)$(?=(?:\r?\n.*+)*?\r?\n\1$)~ <a href="http://nancywalshee03.freehostia.com/regextester/regex_tester.php?seeSaved=4xm5cyfp">Seen here.</a> ~^(.*)$(?=.*\1)~ms <a href="http://nancywalshee03.freehostia.com/regextester/regex_tester.php?seeSaved=ddjcgh5m">Seen here.</a> Quote Link to comment Share on other sites More sharing options...
Grego Posted December 24, 2007 Author Share Posted December 24, 2007 I hope this is what you mean by sample data... Will Match: <tag>Text</tag> <tagt>Text</tagt> Won't Match: <tag>Text</tagt> <tagt>Text</tag> So I need it to look for a closing tag which is the same as the opening tag and match those together, returning the tag name (tag/tagt) and the string in between the tags (Text)... Is that what you wanted to know? If it helps, I was using <notag> as just a name. In retrospect that wasn't a very smart idea... Quote Link to comment Share on other sites More sharing options...
effigy Posted December 25, 2007 Share Posted December 25, 2007 <pre> <?php $data = <<<DATA Will Match: <tag>Text</tag> <tagt>Text</tagt> Won't Match: <tag>Text</tagt> <tagt>Text</tag> DATA; preg_match_all('%<([^>]+)>(.*?)</\1>%', $data, $matches); print_r($matches); ?> </pre> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.