1. Trang chủ
  2. » Công Nghệ Thông Tin

OReilly VBScript In A Nutshell 2nd Edition Mar 2003 ISBN 0596004885

46 80 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 46
Dung lượng 362,26 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

See Also Matches Collection Object, Match Object, RegExp Object... Description The collection of zero or more Match objects returned by theRegExp object's Execute method; each Match obje

Trang 1

Programming Tips and Gotchas

If you're interested only in determining whether the patternstring exists in the search string, there's no point in overridingthe Global property's default value of False

See Also

Matches Collection Object, Match Object, RegExp Object

Trang 2

Description

The collection of zero or more Match objects returned by theRegExp object's Execute method; each Match object allows you

Item

Trang 3

Returns a particular Match object based on index, its

ordinal position in the collection Matches is a zero-basedcollection; that is, its first member is at ordinal position 0,while its last member is at ordinal position Matches.Count -

Trang 4

Description

A member of the Matches collection that is returned by a call tothe RegExp object's Execute method, the Match object

Length

Data Type: Long

Indicates the number of characters in the match found inthe search string This is also the number of characters in

Trang 5

Execute method is then called to search for every filenamebeginning with the letter "B" (the regular expression searchesfor two spaces followed by a "B") The Matches collection isthen iterated so that each filename can be extracted from

Trang 6

See Also

RegExp Object

Trang 7

Description

The RegExp object provides support for regular expression

matchingfor the ability to search strings for substrings matchinggeneral or specific patterns

In order to conduct a pattern search, you must first instantiatethe regular expression object, with code like the following:

Dim oRegExp ' Instance of RegExp object Set oRegExp = New RegExp

To conduct a search using the RegExp object, do the following:

Determine whether the search should be case-sensitive

Determine whether all instances or just the first instance ofthe substring should be returned

Supply the pattern string that you want to find

Provide a string that the RegExp object is to search

The RegExp object allows you to search for a substring thatmatches your pattern string in any of three ways:

You can determine whether a pattern match is found in thestring

You can return one or all of the occurrences of the matchingsubstrings In this case, results are returned in Match

Trang 8

You can replace all substrings matching the pattern stringwith another string

Properties

The RegExp object supports the three properties shown in thefollowing table Each is documented in depth in its own section

Trang 9

library and can be added to any Visual Basic project It is listed

in the References dialog (which is available by selecting theReferences option from the Visual Basic Project menu) as

"Microsoft VBScript Regular Expressions."

See Also

InStr, InStrB Functions, InstrRev Function, Match Object,

Matches Collection Object

Trang 11

Remember to use the Set statement to assign the Matchescollection returned by the Execute method to an objectvariable

You can determine whether the Matches collection returned

by the Execute method is empty by examining its Countproperty It is empty if the value of Count is 0

Trang 12

Syntax

InStr([start, ]stringtosearch, stringtofind[, comparemodestart

Trang 13

stringtosearch is Null Null

stringtofind is zero-length start

stringtofind is Null Null

Trang 14

You must specify a start argument if you are specifying a

stringtofind, as opposed to the character position

VBA/VBScript Differences

Trang 16

Syntax

InstrRev(sourcestring, soughtstring[, start[, compare]])sourcestring

Trang 17

sourcestring ; its value can be vbBinaryCompare or

The default value for compare is vbBinaryCompare

start designates the starting point of the search and is thenumber of characters from the start of the string

If start is omitted, the search begins from the last

character in sourcestring

sourcestring is the complete string in which you want tofind the starting position of a substring

Trang 18

If soughtstring is found within sourcestring, the value

returned by InStrRev is the position of sourcestring from

See Also

InStr, InStrB Functions

Trang 19

Marks the next character as either a special character (such as \n for the newline character) or as a literal (if that character otherwise has special meaning in a pattern search string) The special characters are:

Trang 20

Can be referenced again in the replacement string in calls to the RegExp.Replace method To use the result of the original subexpression as a replacement string, reference its one-based ordinal position among

subexpressions, preceded by a dollar sign (e.g., $1 ) See RegExp.Replace Method for an example.

x|y Matches either x or y

{n} Matches exactly n times, where n is a nonnegative integer.

{n,} Matches at least n times, where n is a nonnegative integer o{1,} is the same as

o+ , and o{0,} is the same as o*

{n,m} Matches at least n and at most m times, where m and n are nonnegative integers.

o{0,1} is the same as o?

Trang 21

[abc] Matches any one of the enclosed characters (represented by abc ) in the character

set.

[^xyz] Matches any character (represented by xyz ) not enclosed in the character set For

example, [^abc] matches the "p" in "plain."

[a-z] Matches any character in a range of characters (represented by a-z ).

[^m-z] Matches any character not included in a range of characters (represented by m-z ).

\b

Matches a word boundary; that is, the position between a word and a space The word boundary symbol does not include newline characters or the end of input (see the \s symbol).

\B Matches a nonword boundary ea*r\B matches the "ear" in "never early."

\xn Matches n , where n is the hexadecimal value of an ASCII code Hexadecimal

escape values must be two digits long.

Trang 22

The following table shows a search string and the Value

property of each Match object returned by the Execute methodwhen the string:

Trang 23

\164\157 To, to, to (3 matches)

\x74\x6f To, to, to (3 matches)

Searches using regular expressions can be quite complex Ifyou're interested in a book that deals exclusively with regular

expressions and pattern searches, see Mastering Regular

Expressions, written by Jeffrey E Friedl (O'Reilly).

Trang 26

Performs a regular expression search against searchString andreplaces matched substrings with replaceString

Rules at a Glance

Trang 27

The method searches searchString using the RegExp

Trang 29

The method returns True if the search succeeds and False

otherwise

Trang 30

Since a search is successful if one match is found, you donot have to set the RegExp object's Global property beforecalling the Test method

You can use the method to determine whether a matchexists before calling either the Execute or the Replace

methods

See Also

RegExp.Execute Method, RegExp.Pattern Property,

RegExp.Replace Method

Trang 32

If length is greater than the length of string, string isreturned

If length is less than 0 or Null, the function generatesruntime error 5, "Invalid procedure call or argument," andruntime error 94, "Invalid use of Null," respectively

If string contains Null, Left returns Null

Trang 34

Rules at a Glance

If string contains a Null, Mid returns Null

If start is more than the length of string, a zero-lengthstring is returned

If start is less than zero, error 5, "Invalid procedure call orargument," is generated

If length is omitted or is greater than the length of string,all characters from start to the end of string are

returned

The MidB version of the Mid function is used with byte data held within a string When using MidB, both start and

length refer to numbers of bytes as opposed to numbers ofcharacters

Public Function ParseString(strString)

Dim arr( )

Dim intStart, intEnd, intStrLen, intCtr

Trang 36

VBA supports the Mid statement, which allows a portion ofthe string to be replaced with another substring For

Trang 38

If length is greater than the length of string, string is

returned

If length is less than zero or is Null, an error is generated

If string contains a Null, Right returns Null

Trang 40

Syntax

StrComp(string1, string2[, compare])string1

Trang 41

Rules at a Glance

The following intrinsic constants are available as thesettings for compare:

Constant Value Comparison to perform

Trang 42

string2 (or vice versa), couldn't you simply use the < or >

comparison operators? When you're dealing with strings ofcharacters, VBScript sees each character as a number

Simply using the comparison operators therefore comparesthe numerical value of one string with the other Take thisscenario:

This is similar to how the default StrComp option

vbBinaryCompare operatescomparing the Unicode numbers

of each string at binary level The sort order is derived fromthe international binary representations of the characters

vbCompareText performs a case-insensitive search, whichmeans that it ignores the difference between upper- andlowercase characters It also means that it will equate

different representations of the same character in some FarEastern character sets vbCompareText, in other words,indicates a case-insensitive textual sort order as determined

by the user's locale

Even performing a simple single comparison like:

If UCase(sString1) < UCase(sString2) Then

Trang 43

more elegant and efficient StrComp function call:

If StrComp(sString1,sString2, vbTextCompare) = -1Then

The former version, though, is easier to read and makes thecode self-documenting

Trang 44

Rules at a Glance

Trang 45

If either string or varname is Null, Len and LenB return

Null

You can't use Len or LenB with an object variable.

If varname is an array, you must also specify a valid

subscript In other words, Len and LenB can't determine thetotal number of elements in or the total size of an array To

Trang 46

differently Len and LenB in VBA reports the number of bytes

required to store the non-string data type in memory In

contrast, in VBScript, Len reports the number of characters in the string representation of non-character data, and LenB

reports the number of bytes needed to store the string

representation of noncharacter data

Ngày đăng: 26/03/2019, 17:13

TỪ KHÓA LIÊN QUAN