30.07.2013 Views

Visual Basic.NET How to Program (PDF)

Visual Basic.NET How to Program (PDF)

Visual Basic.NET How to Program (PDF)

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 15 Strings, Characters and Regular Expressions 677<br />

Fig. 15.23 Regex methods Replace and Split (part 3 of 3).<br />

Method Replace replaces text in a String with new text wherever the original<br />

String matches a regular expression. We present two versions of this method in<br />

Fig. 15.23. The first version (line 21) is Shared and takes three parameters—the String<br />

<strong>to</strong> modify, the String containing the regular expression <strong>to</strong> match and the replacement<br />

String. Here, Replace replaces every instance of "*" in testString1 with "^".<br />

Notice that the regular expression ("\*") precedes character * with a backslash, \. Normally,<br />

* is a quantifier indicating that a regular expression should match any number of<br />

occurrences of a preceding pattern. <strong>How</strong>ever, in line 21, we want <strong>to</strong> find all occurrences of<br />

the literal character *; <strong>to</strong> do this, we must escape character * with character \. By escaping<br />

a special regular expression character with a \, we inform the regular-expression matching<br />

engine <strong>to</strong> find the actual character, as opposed <strong>to</strong> what it represents in a regular expression.<br />

The second version of method Replace (line 26) is an instance method that uses the regular<br />

expression passed <strong>to</strong> the construc<strong>to</strong>r for testRegex1 (line 14) <strong>to</strong> perform the<br />

replacement operation. In this case, every match for the regular expression "stars" in<br />

testString1 is replaced with "carets".<br />

Line 15 instantiates testRegex2 with argument "\d". The call <strong>to</strong> instance method<br />

Replace in line 40 takes three arguments—a String <strong>to</strong> modify, a String containing<br />

the replacement text and an Integer specifying the number of replacements <strong>to</strong> make. In<br />

other words, this version of Replace replaces the first three instances of a digit ("\d")<br />

in testString2 with the text "digit" (line 40).<br />

Method Split divides a String in<strong>to</strong> several substrings. The original String is<br />

broken in any location that matches a specified regular expression. Method Split returns<br />

an array containing the substrings between matches for the regular expression. In line 45,<br />

we use the Shared version of method Split <strong>to</strong> separate a String of comma-separated<br />

integers. The first argument is the String <strong>to</strong> split, and the second argument is the regular<br />

expression. In this case, we use the regular expression ",\s*" <strong>to</strong> separate the substrings<br />

wherever a comma occurs. By matching any whitespace characters, we eliminate extra<br />

spaces from the resulting substrings.<br />

SUMMARY<br />

Characters are the fundamental building blocks of <strong>Visual</strong> <strong>Basic</strong> program code. Every program is<br />

composed of a sequence of characters that is interpreted by the compiler as a series of instructions<br />

used <strong>to</strong> accomplish a task.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!