I have some regex code originally written in PHP and I need to port this to ASP. Here is the original PHP code:
$contents = file_get_contents("http://localhost/source.txt");
$title = 'My Title';
preg_match("/<b>$title.*?(<p.*?)<\/td/smi",$contents,$matches);
print_r($matches);
And here is the ASP ported version:
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>
If I write out the actual regex pattern in both languages, it’s the same except for the escaping of the forward slash character in the PHP version. (I’ve also tested adding this, with the same effect).
Is there some subtle difference in the regexes that I am missing or is it some silly mistake I just can’t see?
Thanks.
There are a number of differences between .NET and PHP regular expressions.1. However ASP-Classic’s use of regular expressions is far older, and less well documented.
Assuming this Scripting Clinic article Microsoft Beefs Up VBScript with Regular Expressions is correct then you will need to re-write your expression.
To start with there is no support for “minimal matching” with
*?,??and+?.1 Compare tables 9-1 and 10-1 of Mastering Regular Expressions (3rd edition).