4.4.2.3–4.4.2.4 - Regular expressions and regular languages (C#)

4.4.2.3–4.4.2.4 - Regular expressions and regular languages (C#)

Describe sets of strings using regular expressions, relate them to finite-state machines and apply simple patterns in C#. This route uses written C# tasks and is incomplete: runnable support pending.

Strings and regular languages

A language is a set of strings, and a regular expression describes which strings belong to that set.

An alphabet is the set of available symbols. Over Σ={a,b}\Sigma=\{a,b\}, strings include aa, bbbb, abaaba and the empty string ε\varepsilon. The empty string has length zero; it is different from a string containing a blank-space character. A language need not be a natural language or a programming language: “all strings of a's and b's beginning with a” is a language.

Regular language

A language is regular if a regular expression can represent it. Equivalently, it is a language accepted by a finite-state machine.

The expression a(a∣b)∗a(a\mid b)^* denotes one initial a followed by zero or more a's or b's. Its language contains a,aa,ab,aaa,aab,aba,abb,…a,aa,ab,aaa,aab,aba,abb,\ldots. It excludes ε\varepsilon, because the initial a is compulsory, and excludes baba, because its first symbol is b. Matching the whole string asks whether it is a member of the language; finding a matching substring asks a different question. The string ba contains the substring a but is not itself in this language.

A regular expression describes a set rather than a particular order of enumeration. There are infinitely many strings in this example, but each individual string is finite and can be checked by a finite-state machine. An infinite set does not require an infinite number of states.

Repetition, choice and grouping

Regular-expression operators specify repetition and alternatives, and grouping determines which expression an operator affects.

NotationMeaningExample language members
a∗a^*Zero or more a'sε,a,aa,…\varepsilon,a,aa,\ldots
a+a^+One or more a'sa,aa,aaa,…a,aa,aaa,\ldots
a?a?Zero or one aε,a\varepsilon,a
a∣ba\mid ba or ba,ba,b
(ab)∗(ab)^*Zero or more repetitions of the pair abε,ab,abab,…\varepsilon,ab,abab,\ldots

Writing adjacent expressions means concatenation. In ab∗ab^*, only b is repeated: a,ab,abb,…a,ab,abb,\ldots. In (ab)∗(ab)^*, the entire pair is repeated. Neither expression describes arbitrary arrangements of a's and b's. The expression (a∣b)∗(a\mid b)^* does: each repetition chooses either symbol independently.

To form a pattern, translate each requirement before combining them. “Begin with a, then at least one b or c, then optionally d” becomes a(b∣c)+d?a(b\mid c)^+d?. The group permits a different choice on each repetition, so abccd is valid. ad is invalid because the middle group must occur at least once; abdd is invalid because d is optional once, not repeated.

Another language consists of one or more occurrences of either the pair ab or the pair ba. Its expression is (ab∣ba)+(ab\mid ba)^+. abba is valid because it is ab followed by ba; aba is invalid because the final a does not form a complete pair. Grouping is a logical part of the solution, not decorative punctuation. Other metacharacters used in an examination question will be explained there.

Continue this lesson.

You’re previewing NovaLearn without an account. Create a free account to read the rest.

Create free account