How to use regular expressions in VB6.0
Recently, VB6 was used to solve a Web page parsing application involving regular expressions. How to use regular expressions in VB6 is described on the MS website, but the introduction is not comprehensive. Next we will summarize the content of the MS website and the items found.
After Microsoft VBScript Regular Expressions 5.5 is referenced, you can declare a regular expression-related object. There are three main objects: Regexp, matchcollection, and match.
1. Regexp
This is the main object for VB to use the regular expression matching mode. The attribute is used to set the modes of strings that are passed to the Regexp instance for comparison. It provides a method to determine whether a string matches a regular expression in a specific pattern.
Attribute:
Pattern
: A string used to define a regular expression.
Ignorecase
: A boolean attribute that indicates whether regular expression tests must be performed on all possible matches in a string. This is the explanation of MS, which is a bit confusing. In actual use, if it is true, the matching of English letter size is ignored. If it is false, the matching is case-insensitive.
Global
: Sets a Boolean value or returns a Boolean value. This Boolean value indicates whether a pattern must match all search items in the entire search string or only the first search item.
Multiline
: This MS is not described. Check the data, set a Boolean value or return a Boolean value, and check whether the search is performed in multiple rows of the string. If multiple lines of text are allowed to be matched, multiline is true. If the search must be stopped in another row, the multiline is false.
Method:
Execute
: Returns a matchcollection object that contains each matched match object.
Replace
: MS is not described. This is a string that replaces the matching character with the specified character.
Test
: Returns a Boolean value indicating whether the regular expression matches the string.
2. matchcollection
Is a collection object that contains information about matching strings. This object contains each matching object that is successfully matched.
Attribute
Count
: Total number of matched objects.
Item
: Index of the matched object.
3. Match
Is a successfully matched object.
Attribute:
Firstindex
: The starting position of the matched string.
Length
: The character length of the matched string.
Submatches
: Child item of the matching result of the matching object.
Value
: The value of the matched object.
Reference Ms Web site: http://support.microsoft.com/kb/818802/zh-cn
. The usage of several attributes and methods not described in MS is shown in the following simple examples:1. the test method of Regexp:
1. the test method of Regexp:
Function
Btest (byval s
As
String
, Byval P
As
String
)
As
Boolean
Dim
Re
As
Regexp
Set
Re
=
New
Regexp
Re. ignorecase
=
False
'
Set case sensitivity
Re. Pattern
=
P
Btest
=
Re. test (s)
End Function
Private
Sub
Commandmediaclick ()
Dim
S
As
String
Dim
P
As
String
S
=
"
My mail: test@163.com. Call us!
"
'
Test whether the string contains numbers:
P
=
"\
D +
"
Msgbox
Btest (S, P)
'
Whether the test string is composed of digits:
P
=
"
^ \ D + $
"
Msgbox
Btest (S, P)
'
Test whether the string contains uppercase letters:
P
=
"
A-Z +
"
Msgbox
Btest (S, P)
End sub
2. The replace method of Regexp:
Function
Strreplace (S
As
String
, P
As
String
, R
As
String
)
As
String
Dim
Re
As
Regexp
Set
Re
=
New
Regexp
Re. ignorecase
=
True
Re. Global
=
True
Re. Pattern
=
P
Strreplace
=
Re. Replace (S, R)
End Function
Private
Sub
Command2_click ()
Dim
S
As
String
'
String
Dim
P
As
String
'
Regular Expression
Dim
R
As
String
'
String to be replaced
'
BelowCodeIs to replace the email address
S
=
"
My E-mail: Test@163.com. Call us!
"
P
=
"
W + @ w +. W +
"
R
=
"
E_Mail@sohu.net
"
S
=
Strreplace (S, P, R)
Debug. Print s
'
Result: My E-mail: E_Mail@sohu.net. Call us!
End sub
3. attributes of the match submatches:
Private
Sub
Command3_click ()
Dim
Re
As
Regexp
Dim
MH
As
Match
Dim
MHS
As
Matchcollection
Dim
Inpstr
As
String
Inpstr
=
"
My E-mail: lucky@163.com. Call us!
"
Set
Re
=
New
Regexp
Re. Pattern
=
"
(W +) @ (W +). (W +)
"
'
It is also the matching address. Note that it is different from the previous example.
Set
MHS
=
Re. Execute (inpstr)
Set
MH
=
MHS (
0
)
'
Only one matching
Debug. Print
"
The email address is:
"
&
Mh. Value
'
Here is the Matching content
Debug. Print
"
The user name is:
"
&
Mh. submatches (
0
)
'
Content in the first bracket
Debug. Print
"
Email:
"
&
Mh. submatches (
1
)
'
Content in the second bracket
Debug. Print
"
The domain name is:
"
&
Mh. submatches (
2
)
'
Content in the third Bracket
End sub