正则表达式在HTML代码中找到标签/属性

时间:2015-10-19 07:41:22

标签: c++ regex pcre

我想找到某些常见的html标签/属性

Table TBL_STD_USER_USERGROUP have field USER_USERGROUP_USER_ID and USER_USERGROUP_CUSTID as int(11)

我想在html代码中找到第一个“href”实例,但不是第二个实例,因为它只是html。

我可以尝试在“< ...>”中查找代码分隔符,但有2个问题,大多数浏览器将允许'<'即使它应该是“GT;”

<a href="xyz">this is an example of an href</a> 

the letter A is < than the letter B

所以我可以安全地查找html中的属性,即使它并不总是使用正则表达式100%正确格式化代码?

1 个答案:

答案 0 :(得分:0)

正则表达式<\s*a\s+(?:\w+\s*=\s*(?:"[^"]*"|'[^']*')\s+)*href\s*=\s*(?:"([^"]*)"|'([^']*)')

将匹配字符串#1,4,5,7,8

1: <a href="xyz">this is an example of an href</a> 

2: <a name="24 is > than 12">this is an example of an href</a>

3: <a name="24 is > than 12">this is an example of an href="xyz"</a>

4: <a href="xyz" name="24 is > than 12">this is an example of an href</a>

5: <a name="24 is > than 12" href="xyz">this is an example of an href</a>

6: <a name="24 is > than 12 href='xyz'">this is an example of an href</a>

7: <a name="24 is > than 12 href='xyz'" href="xyz">this is an example of an href</a>

8: <a name="24 is > than 12 href='xyz'" href="xyz">this is an example of an href="xyz"</a>

通过Regex Online进行测试。

如果使用'引号,您必须使用第二个匹配组(matches[2])。