4.4.2.3–4.4.2.4 - Regular expressions and regular languages (VB.NET)
Describe sets of strings with regular expressions, use them for matching and manipulation in VB.NET, and relate simple expressions to finite-state machines. Source tasks are written-only; this route is incomplete: runnable support pending.
Languages and matching
A language is a set of strings over an alphabet. A regular expression describes such a set using a convenient shorthand. The expression is a description; the language is the collection of strings it describes.
Over alphabet {a, b}, a(a|b)* describes strings beginning with a followed by any number of a or b symbols. Its language includes a, aa, ab, aaa, aab and aba. It excludes the empty string and every string beginning with b. The empty string, often written , has length zero; it is not a space character or the literal word “empty”. A language can contain infinitely many finite strings because repetition has no fixed maximum.
In a theoretical language-membership question the complete string must fit the expression. A library's search operation can instead find a matching substring within a larger string. These are different tasks. In .NET, Regex.IsMatch reports whether a match exists; adding \A at the beginning and \z at the end requires the entire string. Those extra anchors are explained here as library syntax, rather than assumed AQA metacharacter knowledge.
Imports System.Text.RegularExpressions
Module Program
Sub Main()
Console.WriteLine(Regex.IsMatch("ba", "a(a|b)*"))
Console.WriteLine(Regex.IsMatch("ba", "\Aa(a|b)*\z"))
Console.WriteLine(Regex.IsMatch("abb", "\Aa(a|b)*\z"))
End Sub
End Module
The results are True, False and True. The first search finds the final a in “ba”; the anchored search rejects “ba”. In a VB.NET string, a backslash remains a backslash: these regex anchors do not need doubled backslashes in the source. Matching is case-sensitive in these examples and inputs are non-null.
Repetition, choice and native use
An operator applies to the immediately preceding symbol or grouped expression. Grouping therefore changes the language, rather than merely changing its appearance.
| Notation | Meaning | Example and some matching strings |
|---|---|---|
* | zero or more repetitions | a*: empty string, a, aa |
+ | one or more repetitions | a+: a, aa, but not empty |
? | zero or one occurrence | ab?: a or ab |
| | alternation, either choice | a|b: a or b |
( ) | group an expression | (ab)+: ab, abab, ababab |
Concatenation means one expression followed by the next. ab* means one a then zero or more b symbols, whereas (ab)* repeats the entire two-symbol group and includes the empty string. a(b|c)d gives abd or acd; the choice does not make a or d optional. a+ is equivalent to aa*, and a? chooses between one a and no a.
For a code beginning with c, then one or more a/b symbols, then an optional d, use c(a|b)+d?. Whole-string examples are ca, cbad and cabb; c and cadb fail. Translate the rule into required pieces, then check minimum length, optional parts and an invalid symbol.
The same notation can select text to manipulate. Regex.Replace("baaaabaa", "a+", "a") produces “baba”: each nonempty run of a symbols is replaced by one a. This is a substring-replacement operation, so it deliberately has no whole-string anchors. Replacing matches changes text; IsMatch only returns a Boolean.
Continue this lesson.
You’re previewing NovaLearn without an account. Create a free account to read the rest.
Create free account