.net Regex帮助不需要为我工作
我有一个源字符串.net Regex帮助不需要为我工作,.net,regex,vb.net,.net,Regex,Vb.net,我有一个源字符串 <img src="./CaptchaServlet?rd=htb54m" class="flt" id="captcha" height="33" width="110"/> 试试这个: CaptchaServlet\?rd=[^"]* …只要双引号从不作为数据的一部分出现,这将起作用。:) 我强烈建议您检查一下,它将在测试.net正则表达式时真正帮助您 编辑:改进了正则表达式。以前它只适用于字母数字字符 描述 您编写的表达式CaptchaServlet?r
<img src="./CaptchaServlet?rd=htb54m" class="flt" id="captcha" height="33" width="110"/>
试试这个:
CaptchaServlet\?rd=[^"]*
…只要双引号从不作为数据的一部分出现,这将起作用。:)
我强烈建议您检查一下,它将在测试.net正则表达式时真正帮助您
编辑:改进了正则表达式。以前它只适用于字母数字字符 描述
您编写的表达式CaptchaServlet?rd=*(.+?)“”
有几个错误:
- 第一个
表示将前面的?
设置为可选。我想你真的想把问号变成一个字面问号,所以你需要用t
\?
- 等号后面的
也意味着允许*
显示零次或更多时间,最多无限次。这有点含糊不清,如果源字符串可能有1或0个等号,那么您可能希望将该=
替换为=*
,这只会使=?
成为可选的=
- 捕获src属性值
- 使用双引号、单引号和非引号值
- 避免了通常会导致简单表达式出错的棘手边缘情况
)(?:[^>=]|='[^']*'.[^']*'.[^']*'.[^']+[^\s>]*)*>
或者,如果您希望只提取rd
查询字符串值,则可以使用:)(?:[^>=]|='[^']*'.[^']*'.=“[^']*.[^'][^\s>]*>
。这将把xxxxxx放入捕获组2中
例子
示例文本
请注意,前两个图像标记有一些非常困难的边缘情况
<img onmouseover=' img = 10; src="NotYourImage.png" ; if (3 <img && src="NotYourImage.png" && 6>3) { funRotate(src) ; } ; ' src="ImageYouAreLookingFor.png">
<img onmouseover=' src="NotTheDroidsYouAreLookingFor.png" ; if (x > 3) { funRotate(src); } ' src="http://another.example/picture.png">
<img src="./CaptchaServlet?rd=htb54m" class="flt" id="captcha" height="33" width="110"/>
3){funRotate(src);};'src=“imageyouarelokingforce.png”>
3) {funRotate(src);}'src='http://another.example/picture.png">
VB.Net Exmaple
Imports System.Text.RegularExpressions
Module Module1
Sub Main()
Dim sourcestring as String = "replace with your source string"
Dim re As Regex = New Regex("<img(?=\s|>)(?=(?:[^>=]|='[^']*'|=""[^""]*""|=[^'""][^\s>]*)*?\ssrc=(['""]?)\.\/CaptchaServlet\?rd=(.*?)\1(?:\s|>))(?:[^>=]|='[^']*'|=""[^""]*""|=[^'""][^\s>]*)*>
",RegexOptions.IgnoreCase OR RegexOptions.IgnorePatternWhitespace OR RegexOptions.Multiline OR RegexOptions.Singleline)
Dim mc as MatchCollection = re.Matches(sourcestring)
Dim mIdx as Integer = 0
For each m as Match in mc
For groupIdx As Integer = 0 To m.Groups.Count - 1
Console.WriteLine("[{0}][{1}] = {2}", mIdx, re.GetGroupNames(groupIdx), m.Groups(groupIdx).Value)
Next
mIdx=mIdx+1
Next
End Sub
End Module
导入System.Text.RegularExpressions
模块1
副标题()
Dim sourcestring as String=“替换为源字符串”
Dim re As Regex=New Regex(“)(?:[^>=]|='[^']*'|=''[^']*''“[^']*''”[^\s>]*>
“,RegexOptions.IgnoreCase或RegexOptions.IgnorePatternWhitespace或RegexOptions.Multiline或RegexOptions.Singleline)
Dim mc as MatchCollection=re.Matches(sourcestring)
Dim mIdx为整数=0
对于mc中的每个m as匹配
对于groupIdx,整数=0到m.Groups.Count-1
Console.WriteLine(“[{0}][{1}]={2}”、mIdx、re.GetGroupNames(groupIdx)、m.Groups(groupIdx.Value)
下一个
mIdx=mIdx+1
下一个
端接头
端模块
匹配
组0获取整个图像标记组1获取用于包围src属性的引号,并用于确保匹配正确的结束引号
组2获取src值,或者如果使用上面的备用正则表达式,则只接收rd查询字符串
[0][0] = <img onmouseover=' img = 10; src="NotYourImage.png" ; if (3 <img && src="NotYourImage.png" && 6>3) { funRotate(src) ; } ; ' src="ImageYouAreLookingFor.png">
[0][1] = "
[0][2] = ImageYouAreLookingFor.png
[1][0] = <img onmouseover=' src="NotTheDroidsYouAreLookingFor.png" ; if (x > 3) { funRotate(src); } ' src="http://another.example/picture.png">
[1][1] = "
[1][2] = http://another.example/picture.png
[2][0] = <img src="./CaptchaServlet?rd=htb54m" class="flt" id="captcha" height="33" width="110"/>
[2][1] = "
[2][2] = ./CaptchaServlet?rd=htb54m
[0][0]=3{funRotate(src);};'src=“imageyouarelokingforce.png”>
[0][1] = "
[0][2]=ImageYouareLooking.png
[1] [0]=3{funRotate(src);}'src=”http://another.example/picture.png">
[1][1] = "
[1][2] = http://another.example/picture.png
[2][0] =
[2][1] = "
[2] [2]=。/CaptchaServlet?rd=htb54m
CaptchaServlet?rd=([^”“]*)
这不起作用:(我已经编辑了你的标题。请看“”,其中的共识是“不,他们不应该”。@Arun sankar是因为引用吗?你只需要避开它。
[0][0] = <img onmouseover=' img = 10; src="NotYourImage.png" ; if (3 <img && src="NotYourImage.png" && 6>3) { funRotate(src) ; } ; ' src="ImageYouAreLookingFor.png">
[0][1] = "
[0][2] = ImageYouAreLookingFor.png
[1][0] = <img onmouseover=' src="NotTheDroidsYouAreLookingFor.png" ; if (x > 3) { funRotate(src); } ' src="http://another.example/picture.png">
[1][1] = "
[1][2] = http://another.example/picture.png
[2][0] = <img src="./CaptchaServlet?rd=htb54m" class="flt" id="captcha" height="33" width="110"/>
[2][1] = "
[2][2] = ./CaptchaServlet?rd=htb54m