Jamers Posted December 31, 2016 Report Share Posted December 31, 2016 这是普通匹配的,PHP中能够使用: '%<(?<tag>[\w]+)([^>]*)>(.*?)</\k<tag>>%i' 这是递归匹配的,使用了正则的平衡组,PHP5.6的版本中还不运行,我测试了一下在C# .net中是支持的,希望PHP7.0的时候能够支持这种正则吧。 <(?<tag>[\w]+)([^>]*)>(.*?)((?<rept><\k<tag>([^>]*)|</\k<tag>>(?<-rept>)|.*?)*)</\k<tag>> 测试字符串: <a><b>test<b></a> <div>info1<div>info2</div></div> 匹配结果: Match 1: <a><b>test<b></a> 0 17 Group "tag": a 1 1 Group 1: 2 0 Group 2: 3 0 Group 3: <b>test<b> 3 10 Group "rept": 13 0 Group 4 did not participate in the match Match 2: <div>info1<div>info2</div></div> 19 32 Group "tag": div 20 3 Group 1: 23 0 Group 2: 24 0 Group 3: info1<div>info2</div> 24 21 Group "rept": 45 0 Group 4: 33 0 Link to comment Share on other sites More sharing options...
Jamers Posted June 9, 2017 Author Report Share Posted June 9, 2017 分组匹配: '%<(title|span)>(.*)</\1>%si' <title>st<span>test</span></title> 闭合匹配,可以继续匹配内部数据,目前是优先匹配末级。 Link to comment Share on other sites More sharing options...
Recommended Posts
Please sign in to comment
You will be able to leave a comment after signing in
Sign In Now