HOW TO: Using regular expressions in Microsoft Visual Basic 6.0 [transfer from MSDN]

xiaoxiao2021-03-06  15

This task content

•Summary

• Requirements • Use Regular Expression Matching Mode • Separation Example • Reference

This page

Summary reference

summary

This article describes how to create a regular expression and how to use a regular expression to determine if the string matches a specific mode. With regular expressions, you can easily analyze strings and match the string with a specific mode. If you use the available objects in the Microsoft VBScript Regular Express 5.5 library, you can compare a string with a specific mode. Use a mode to replace another mode, or only retrieve a formatted string Some parts. This article describes how to construct a pattern to analyze a string containing a plurality of instances of the same mode.

Back to top

Claim

The following list introduces the recommended hardware, software, network structure, and service pack required:

• Microsoft Visual Basic 6.0 This article assumes that you are familiar with the following topics:

• Visual Basic 6.0 • Regular expression syntax

Back to top

Use regular expression matching mode

In Visual Basic 6.0,

Regexp objects use regular expressive matching mode. Below is

The properties provided by RegexP. These properties are used to set those to compare

The mode of the string of the regexp instance.

• Pattern: A string that defines the regular expression. • IgnoreCase: A Boolean value indicating whether all possible matches in a string must be tested. • Global: Sets a Boolean value or returns a Boolean value that indicates that a mode must match all search items in the entire search string or only match the first search term. • Regexp: Provides the following method to determine if the string matches the specific mode of the regular expression:

• TEST: Returns a Boolean value indicating whether the regular expression is successfully matched with the string. • EXECUTE: Returns a MatchCollection object that contains each successful matching MATCH object. To match a string with a regular expression, follow these steps:

1. Set the regular expression using the "Pattern" method of the "regexp" object. 2. Get the string to check with this mode. 3. Set the "IgnoreCase" property of the "regexp" object to True. 4. Passing the string you got in step 2 to the "execute" method of the "regexp" object as a parameter. 5. Specify the return value of the "Execute" method to the "MatchCollection" object. "MatchCollection" object contains information about matching strings.

Note that you can also use

Test method to check if the string matches a specific regular expression.

Back to top

Step-by-step example

1. Start Microsoft Visual Basic 6.0.2. On the File menu, click New Project. 3. In the New Project dialog box, click Standard EXE, and then click OK. Form1.4 will be created by default. Click References on the Projects menu. 5. Double-click "Microsoft Vbscript Regular Expressions 5.5" and click OK. 6. In the toolbox, double-click the command button. By default, "Command1" will be added to the form. 7. Double-click "Command1" to open the code window. 8. Paste the following code to the "Command1_Click" event handler: msgbox (TestReGexp ("IS2 IS3 IS4")) Note that the "IS1 IS2 IS3 IS4" string checks IS. Mode in this example . You can use this special character (.) As a wildcard, so that the search mode can match more than one character. If you add two stations in your search mode, you will see two other characters. If you don't use any period, you will only see the search mode. 9. After adding the following functions to the "Command1_Click" event handler: Function TestRegeXP (MyPattern as String, MyString AS String) 'Create Objects.

DIM Objregexp as regexp

DIM Objmatch as match

DIM colmatches as matchcollection

DIM RETSTR AS STRING

'Create a regular expression Object.

Set objRegexp = new regexp

'Set the pattern by using the pattern property.

Objregexp.pattern = mypattern

'Set case insensitivity.

Objregexp.Ignorecase = TRUE

'Set Global Applicability.

Objregexp.global = true

'Test WHETHER THE STRING CAN BE Compared.

IF (bString) = true) THEN

'Get the matches.

Set colmatches = objRegexp.execute (mystring) 'Execute Search.

For Each Objmatch in colmatches' Iterate Matches Collection.

Retstr = Retstr & "Match Found At position"

Retstr = Retstr & Objmatch.firstindex & ". Match value is'"

Retstr = Retstr & Objmatch.Value & "'." & Vbcrlf

NEXT

Else

Retstr = "String matching failed"

END IF

TestRegexp = RETSTR

End function10. On the Run menu, click Start to run the application. 11. Click "Command1". A message box appears, which displays all IS matching items in the IS1 IS2 IS3 IS4 string. Back to top

reference

For more information, please visit the following MSDN Web site:

http://msdn.microsoft.com/library/en-us/script56/html/jsgrpregexpsyntax.asp

Http://msdn.microsoft.com/library/en-us/script56/html/reconintroductiontoregularExpressions.asp

转载请注明原文地址:https://www.9cbs.com/read-46437.html

New Post(0)