Author: A Lai (Email: A at lai.com.cn homepage: http://www.9499.net blog: http://blog.csdn.net/laily)
Keywords: Regular Expression VB VBScript Javascript
Abstract: The conversion from a JavaScript regular expression to a VBScript regular expression also describes the usage of a regular expression in VB.
Previously, I used JavaScript to write a regular expression algorithm that splits a path into a root directory and a subdirectory string. Because the project needs to write the same function using VBScript. The following is the original jsscript program:
// Purpose: to setparate the path string into two part: the root and the subfolder
// IE.
// C:/AA/BB/CC => C:/+ AA/BB/CC
/// AA/BB/CC => // AA + BB/CC
/// Http://www.9499.net => http: // + www.9499.net
VaR strroot, strsub
VaR Re =/^ ([^/] + [///] + | ///[^/] +) (. *) $/
If (Re. Test (strfolder ))
{
Strroot = Regexp. $1
Strsub = Regexp. $2
}
Because the Javascript language integrates the regular expression function, it feels smooth and natural to write. In VB6, the Regular Expression Function of VBScript can be called only through component objects. In the IDE of VB6, select project> references and find and select Microsoft VBScript regular expression. Note that there are two versions available, one is 1.0 and the other is 5.5. Of course we chose 5.5. The difference between the two versions is that the sub-match submatchs function is missing from the 1.0 version. If you select 1.0, you cannot implement the functions of the above JavaScript code, because the key is to use submatchs matching.
The following is the program code of VBScript which has the same functions as the preceding JScript:
'Purpose: To setparate the path string into two part: the root and the subfolder
'Ie.
'C:/AA/BB/CC => C: // + AA/BB/CC
'// AA/BB/CC => // AA + BB/CC
'Http://www.9499.net => http: // + www.9499.net
Dim strroot, strsub
Dim regpathparse
Dim rematch, rematchcol, resubmatch
Re. pattern = "^ ([^/] + [///] + | ///[^/] + )(. *) $"
Set rematchcol = Re. Execute (strfolder)
If rematchcol. Count = 1 then
Set rematch = rematchcol (0)
Set resubmatch = rematch. submatches
If resubmatch. Count = 2 then
Strroot = resubmatch (0)
Strsub = resubmatch (1)
End if
End if
How about JScript Regular Expression writing is much simpler than vbs.