Complete usage of regular expressions in VBS original

VBS regular expression function
Mainly used in ASP, the effect is obvious
1. Form verification function

Copy codeThe code is as follows:

Function Validate(strng,patrn) 
 Dim regEx 
 Set regEx = New RegExp 
 regEx.Pattern = patrn 
 regEx.IgnoreCase = True 
 regEx.Global = True 
 Validate = regEx.test(strng) 
 Set regEx = Nothing 
End Function 

Use examples

  If Validate(Fdr.Name,”Fd{4}_Pd{4}”)=True Then
     … …
  End If


[Ctrl + a full selection note: you need to refresh the page to import external JS]

2. Replace function

Copy codeThe code is as follows:

‘==========================
‘replace with regular expressions
‘==========================
function replaceregex(patern,str,tagstr)
dim regex,matches
set regex=new regExp
regex.pattern=patern
regex.IgnoreCase=true
regex.global=true
matches=regex.replace(str,tagstr)
replaceregex=matches
end function

3. UBB function
www.jb51.net/html/200608/6/574.htm

The following is the content of the author: LCX

I haven’t studied well before. I’ll tidy it up this time.

/ D + in regular is to represent one or more numbers. Use this as an example.

Regexp is to create regular objects. For example, set regex = new regexp. regEx. Pattern is used to set regular patterns, such as

regEx. Pattern =”/d+”。 regEx. Ignorecase = true ‘sets whether it is case sensitive. regEx. Global = true ‘set full availability.

There are three methods for regexp objects: execute, test and replace.

The test method performs a regular expression search on the specified string and returns a Boolean value indicating whether a matching pattern is found. RegExp. The global attribute has no effect on the test method. If a matching pattern is found, the test method returns true; Otherwise, false is returned.

example:

Copy codeThe code is as follows:

Function RegExpTest(patrn, strng)
Dim regex, retval ‘create variable.
Set regex = new regexp ‘establish a regular expression.
regEx. Pattern = pattern ‘sets the pattern.
regEx. Ignorecase = false ‘sets whether it is case sensitive.
retVal = regEx. Test (strng) ‘execute the search test.
If retVal Then
Regexptest = “one or more matches found.”
Else
Regexptest = “no match found.”
End If
End Function
MsgBox(RegExpTest(“d+”, “abcd1234”))
MsgBox(RegExpTest(“d+”, “abcd”))

The replace method replaces the text found in the regular expression lookup, for example:

Copy codeThe code is as follows:

Function ReplaceTest(patrn, replStr)
Dim regex, STR1 ‘create variable.
str1 = “dog 123.”
Set regex = new regexp ‘establish a regular expression.
regEx. Pattern = pattern ‘sets the pattern.
regEx. Ignorecase = true ‘sets whether it is case sensitive.
ReplaceTest = regEx. Replace (STR1, replstr) ‘.
End Function

Msgbox (replacetest (“ D +”, “cat”)) ‘replace 123 in the string with cat

The execute method performs a regular expression search on the specified string. This also involves match objects and matches sets. The matches set is the object set of match. The matches collection contains several independent match objects that can only be created using the execute method of the regexp object. example:

Copy codeThe code is as follows:

Function RegExpTest(patrn, strng)
Dim regex, match, matches’ create variables.
Set regex = new regexp ‘establish a regular expression.
   regEx. Pattern = pattern ‘sets the pattern.
   regEx. Ignorecase = true ‘sets whether it is case sensitive.
   regEx. Global = true ‘set full availability.
   Set Matches = regEx. Execute (strng) ‘execute the search.
For each match in matches’ traverse the matches set.
      RetStr = RetStr & Match. Firstindex & “. The matching length is” & “”
RetStr = RetStr & Match.Length &” “
Retstr = retstr & matches (0) & ”
Retstr = retstr & matches (1) & ”
    RetStr = RetStr & Match. Value & “” ‘array with values 123 and 44
     RetStr = RetStr & vbCRLF
   Next
   RegExpTest = RetStr
End Function
MsgBox(RegExpTest(“d+”, “123a44”))

Read more here: Source link