Jump to content
新域网络技术论坛

正则闭合匹配


Jamers
 Share

Recommended Posts

这是普通匹配的,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

  • 5 months later...

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
 Share

×
×
  • Create New...