正则表达式匹配第一个结束HTMl标记 [英] Regex to match the first ending HTMl tag

查看:367
本文介绍了正则表达式匹配第一个结束HTMl标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 < form。* name =我想写一个匹配第一个结尾标签的正则表达式。登录表单 * GT; [^〜] * LT; /形式> 

上面的正则表达式匹配直到从第8行到第8行的第二个正则表达式,但是我想要一个匹配的正则表达式在下面的例子中,标签的即时结尾应该与第5行匹配。

 < html> 
< body>
< form method =postname =loginForm>
< input type =textname =userName/>
< / form>
< form method =postname =signupForm>
< input type =textname =userName/>
< / form>
< / body>
< / html>


解决方案

只要让模式非贪婪,可能最小的字符数量,而不是最大可能的:

 < form [^>] * name =loginForm [^>] *> [^〜] * LT; /形式> 

编辑:

已更改。* [^>] * ,这样它就不会与标签外部匹配。


I am trying to write a regex which match the first ending form tag.

  <form.*name="loginForm".*>[^~]*</form> 

The above regex matches till the second from ends i.e till line 8. but I want a regex that matches the immediate ending from tag in the below example it should match line 5.

<html>
<body>
<form method = "post" name="loginForm" >
<input type="text" name="userName"/>
</form>
<form method = "post" name="signupForm" >
<input type="text" name="userName"/>
</form>
</body>
</html>

解决方案

Just make the pattern non-greedy so that it matches the smallest possible amount of characters instead of the largest possible:

<form[^>]*name="loginForm"[^>]*>[^~]*?</form>

Edit:
Changed .* to [^>]* in the form tag, so that it doesn't match outside the tag.

这篇关于正则表达式匹配第一个结束HTMl标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆