Regex Design and Algebra Explained: Identities, Arden's Theorem, and Worked Examples

Build regular expressions from language constraints, simplify them with safe identities, prove the result, and solve a recursive equation with Arden's theorem.

KnowledgeGate Team

Exam prep & CS education

Updated 24 Oct 20265 min read

Recognising a regular expression after seeing it is much easier than building one from a language condition. The real difficulty is deciding what each part must guarantee, then proving that the finished expression accepts all and only the required strings. This method starts with language constraints, applies a small set of valid identities, checks boundary strings, and solves one recursive equation. It covers classical regular expressions over finite alphabets, a foundation within core computer science subjects.

Read a regex as a language before manipulating it

Fix the alphabet as Sigma = {0,1}. The atomic expression empty-set denotes no strings, while epsilon denotes the language containing exactly one string, the length-zero string. The atoms 0 and 1 each denote a one-symbol language. Larger expressions use union R|S, concatenation RS, and Kleene star R*. Star permits zero or more repetitions, so (empty-set)* = {epsilon} even though empty-set itself contains nothing.

The usual precedence is star first, concatenation second, and union last. Thus 0|10* means 0|(1(0*)) and denotes {0, 1, 10, 100, 1000, ...}. By contrast, (0|1)0* also contains 00. That shortest witness proves the two expressions are not equivalent.

The regex algebra that is safe to use

Each valid identity is an equality between languages, not a rule for rearranging symbols by appearance.

Law

Language meaning

Example

R|empty-set = R

Union with nothing adds nothing

0|empty-set = 0

R|R = R

Repeating a choice adds no strings

1|1 = 1

Repsilon = epsilonR = R

The empty string changes no concatenation

0epsilon = 0

Rempty-set = empty-setR = empty-set

Concatenation needs a word from both sides

1empty-set = empty-set

R(S|T) = RS|RT

Left concatenation distributes over union

0(1|epsilon) = 01|0

(R|S)T = RT|ST

Right concatenation distributes over union

(0|1)0 = 00|10

R*R* = R*

Two sequences of R blocks remain one sequence

0*0* = 0*

(R*)* = R*

Grouping sequences of blocks adds nothing

(1*)* = 1*

Concatenation is associative but not commutative. With R=0 and S=1, RS=01, whereas SR=10. There is also no general cancellation law. Removing a common-looking fragment can change which strings the expression denotes.

Now simplify E=(epsilon|00*)0*10*10*. First, 00* represents every non-empty all-zero string, so epsilon|00* = 0*. Next, 0*0* = 0*. Therefore E=0*10*10*.

Design from constraints: exactly two 1s

Let L={w in {0,1}* : w contains exactly two occurrences of 1}. Any word in L has three ordered regions of zeros: before the first 1, between the two 1s, and after the second 1. Every region may be empty, so each becomes 0*. The two required events stay as literal 1s. This gives R=0*10*10*.

Test the boundaries. The string 11 uses zero-block counts (i,j,k)=(0,0,0). The string 101 uses (0,1,0), and 001000100 uses (2,3,2). All three match. The strings epsilon and 000 have no 1, while 10101 has three, so none of them match.

Prove the worked regex and trace one full string

For soundness, every word generated by 0*10*10* has the form 0^i 1 0^j 1 0^k for some i,j,k >= 0. The three variable blocks contain only zeros, and the two fixed symbols are 1, so the word belongs to L. For completeness, any binary word with exactly two 1s has a unique split into the zeros before, between, and after those positions. It therefore matches 0*10*10*.

Parse 001000100 from left to right as 00 | 1 | 000 | 1 | 00. Hence i=2, j=3, and k=2; its total length is 2+1+3+1+2=9, and it contains exactly two 1s. The same form cannot represent 10101: its three 1 positions cannot fit into two fixed 1s and zero-only blocks.

Exactly two 1s split into three zero blocks. Show one horizontal worked string 001000100 partitioned as five labelled boxes 00 | 1 | 000 | 1 | 00; label the zero boxes 0^i, i=2, 0^j, j=3, and 0^k, k=2; label the two single-symbol boxes first required 1 and second required 1; underneath show the general form 0^i 1 0^j 1 0^k, i,j,k >= 0 and the regex 0*10*10*; also show length = 2+1+3+1+2 = 9 and number of 1s = 2.

Solve a recursive regex equation with Arden's theorem

Arden's theorem has a direction. If X=AX|B and epsilon is not in L(A), the unique solution is X=A*B. For the right-recursive form X=XA|B, the corresponding solution is X=BA*. Swapping those results would reverse the order of concatenation.

For X=0X|1, take A=0 and B=1. Since epsilon is not in {0}, Arden's theorem gives X=0*1. Starting from X_0=empty-set, substitution produces X_1={1}, X_2={1,01}, and X_3={1,01,001}. Continuing adds one more leading zero each time, so the limit is {0^n1 : n>=0}.

Arden theorem unrolling for X=0X|1. Draw a four-row table with X_0 = empty-set, X_1 = {1}, X_2 = {1,01}, and X_3 = {1,01,001}; use arrows labelled prefix one 0, then union {1} between successive rows; finish with a box limit X = 0*1 = {0^n1 : n >= 0}; include a side note A=0, B=1, epsilon not in L(A).

Disprove false equivalence with a short witness

Proving equality requires both language inclusions. Disproving it needs only one string in one language but not the other. For 0*1 and 10*, the witness 01 belongs only to the first. For (0|1)* and 0*|1*, the same witness belongs to the first but to neither the all-zero nor the all-one branch of the second.

Star scope causes another common error. The string 0011 is accepted by 0*1*, but not by (01)*, whose words must repeat the complete block 01. Parentheses determine exactly which expression the star repeats.

An automata-based equivalence check is the natural next method in Finite Automata: DFA vs NFA and Subset Construction. For proof-oriented practice, continue with regular expressions and the pumping lemma.

How exam-style questions test regex design and algebra

Exam-style questions usually take one of four reusable forms: translate a verbal constraint, simplify with named identities, separate two expressions with a witness, or solve X=AX|B or X=XA|B using Arden's theorem. For example, among 0*10*10*, (0|1)*11(0|1)*, and 0*1(0|1)*1, only the first enforces exactly two 1s. The others can accept strings with additional 1s.

After learning the method, use solved regex and finite-automata equivalence questions to practise translation, equivalence, and witness selection. Treat each expression semantically before trying to manipulate its symbols.

Short version and the next practice step

Translate constraints into ordered regions, manipulate expressions only with valid language identities, and verify the result with a proof plus boundary strings. The anchor result is 0*10*10* for exactly two 1s. Arden's theorem gives 0*1 for X=0X|1.

For a complete subject path, use the Theory of Computation / Automata Theory course. The Zero to Hero complete CS course provides the broader computer science sequence.