< Free Open Study > |
9.5 Support FunctionsBesides the convenience functions described in the previous section, there are a few other static support functions:
Match.Empty
Dim SubMatch as Match = Match.Empty 'Initialize, in case it's not set in the loop below . . . Dim Line as String For Each Line in EmailHeaderLines 'If this is the subject, save the match info for later . . . Dim ThisMatch as Match = Regex.Match(Line, "^Subject:\s*(.*)", _ If the string array EmailHeaderLines actually has no lines (or no Subject lines), the loop that iterates through them won't ever set SubMatch, so the inspection of SubMatch after the loop would result in a null reference exception if it hadn't somehow been initialized. So, it's convenient to use Match.Empty as the initializer in cases like this.
Regex.Escape(
string
)
For example, if you have input from the user in the string variable SearchTerm, you might use it to build a regex with: Dim UserRegex as Regex = New Regex("^" & Regex.Escape(SearchTerm) & "$", _ This allows the search term to contain regular-expression metacharacters without having them treated as such. If not escaped, a SearchTerm value of, say, ':-)' would result in an ArgumentException being thrown (see Section 9.3.1.2).
Regex.Unescape(
string
)
Character shorthands are also decoded. If the original string has '\n', it's actually replaced with a newline in the returned string. Or if it has '\u1234', the corresponding Unicode character will be inserted into the string. All character shorthands listed at the top of the Section 9.1 are interpreted. I can't imagine a good regex-related use for Regex.Unescape, but it may be useful as a general tool for endowing VB strings with some knowledge of escapes.
Regex.CompileToAssembly(···)
|
< Free Open Study > |