移植Regex代码


Porting Regex Code

我有一些regex代码最初是用PHP编写的,我需要将其移植到ASP。以下是原始PHP代码:

$contents = file_get_contents("http://localhost/source.txt");
$title =  'My Title';
preg_match("/<b>$title.*?(<p.*?)<'/td/smi",$contents,$matches);
print_r($matches);

这是ASP移植的版本:

contents = File_Get_Contents("http://localhost/source.txt")
response.write contents
title = "My Title"
regex = "<b>" + title + ".*?(<p.*?)</td"
Set objRE2 = New RegExp
With objRE2
    .Pattern    = regex
    .IgnoreCase = True
    .Global     = True
    .MultiLine  = True
End With
Set myMatches = objRE2.Execute(contents)
'myMatches - Count is 0
Function File_Get_Contents(strFile)
    ' Remote File
    If Left(strFile, 7) = "http://" Or Left(strFile, 8) = "https://" Then
        'Set objXML = Server.CreateObject("Microsoft.XMLHTTP")
        ' Use this line if above errors
        Set objXML = Server.CreateObject("MSXML2.ServerXMLHTTP")
        objXML.Open "GET", strFile, False
        objXML.Send()
        File_Get_Contents = objXML.ResponseText
        Set objXML = Nothing
    ' Local File
    Else
        Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
        Set objFile = objFSO.OpenTextFile(strFile, 1)
        File_Get_Contents = objFile.ReadAll()
        Set objFile = Nothing
        Set objFSO = Nothing
    End If
End Function

source.txt:

<td>
<b>My Title</b><br>
<p>My Content</p></td>

如果我用两种语言编写实际的regex模式,除了PHP版本中的正斜杠字符转义之外,它是相同的。(我也测试过添加这个,效果相同)。

我缺少的正则表达式中有什么细微的差异吗?还是我看不见的愚蠢错误?

谢谢。

.NET和PHP正则表达式之间有很多区别1。然而,ASPClassic对正则表达式的使用要古老得多,而且文档也不多。

假设这篇脚本诊所文章Microsoft Beefs Up VBScript with Regular Expressions是正确的,那么您将需要重写表达式。

首先,不支持与*???+?进行"最小匹配"。


1比较Mastering Regular Expressions(3rd版本)的表9-1和10-1。