Introduction
A regular expression (RE) is a concise notation for a language built from single symbols by union, concatenation and Kleene star. The languages denoted by regular expressions are exactly the regular languages, which are also exactly the languages recognized by finite automata (proved in Equivalence of Regular Expressions and Finite Automata).
In GATE, regular expressions appear as: simplify an expression, decide whether a string matches, convert between RE and DFA/NFA, or identify which expression denotes a given language. This note covers syntax, semantics, precedence, algebraic identities and worked matching examples. Conversion algorithms live in the equivalence note.
Prerequisites
- Alphabets, Strings and Languages: , concatenation, star, versus .
Core concepts
Inductive definition
Fix an alphabet . The regular expressions over and the language of each expression are defined together.
Base cases.
- is a regular expression, and .
- is a regular expression, and .
- For every , is a regular expression, and .
Inductive cases. If and are regular expressions, then so are , and , with
Some texts write or for union; these notes use , matching Hopcroft-Motwani-Ullman.
Parentheses. Parentheses are part of the syntax. They may be dropped using the precedence rules below.
Precedence and associativity
Operator precedence, from highest to lowest:
- Kleene star (and usually , if used as shorthand for ).
- Concatenation (juxtaposition).
- Union .
Star is unary postfix and associates to the left in stacks of stars (). Concatenation and union are associative: and . Union is commutative; concatenation is not.
Examples of parsing.
| Written | Parsed as | Not |
|---|---|---|
| star of union |
Misreading as is an error to avoid: the first language is ; the second is all strings over .
Shorthand
- (one or more).
- (optional), used in some tools; not required in the formal definition.
- means concatenated times ().
Unix-style character classes and wildcards (., [0-9]) are Related material from lexical analysis tools; the formal GATE RE uses only , , symbols, , concatenation and .
How it works
Evaluating membership
To decide whether , expand the expression by the inductive definition, or convert to an NFA and run it (see the equivalence note). For small expressions, case analysis works:
- iff or .
- iff for some , .
- iff for some and each (the case gives ).
Algebraic identities
The following hold for all regular expressions (language equality):
| Identity | Form |
|---|---|
| Associativity | , |
| Commutativity of | |
| Identities | , |
| Annihilator | |
| Idempotence | |
| Distributivity | , |
| Star | , , , |
| More star | , |
These are proved by induction on expressions or by automata equivalence. The last row is useful when simplifying nested stars.
Test for an identity. A law whose variables stand for arbitrary languages holds iff it holds after replacing each variable by a distinct fresh symbol and comparing the two resulting concrete expressions (Hopcroft-Motwani-Ullman, Section 3.4.7). So is a law because over , and is not, because abab is in but not in .
Equivalence traps
| Claim | True? | Witness or reason |
|---|---|---|
| yes | every string is blocks of s and s | |
| yes | cut before each | |
| yes | shifting law | |
| yes | both are odd numbers of s | |
| , , | yes | star absorbs |
| yes | ||
| no | ab is only on the left | |
| no | aab only right, abab only left | |
| no | a only right: is or ends in | |
| yes | an before a later forces an adjacent ab | |
| no | both contain , so the intersection is |
Writing expressions for standard languages
Over :
| Language | One correct expression |
|---|---|
| even length | |
| length divisible by 3 | |
| exactly two s | |
| at most one | |
| even number of s | |
| no two consecutive s | |
does not contain ab | |
| first and last symbols differ | |
ends with ab, alternative form | where |
Counting check. The number of strings of length with no two consecutive s is the Fibonacci number (with ): 1, 2, 3, 5, 8, 13 for . A quick count of short strings is a reliable way to catch a wrong expression.
Algorithms and pseudocode
Simplification is not unique; a practical greedy strategy:
SIMPLIFY-RE(r)
1 rewrite nested stars: (r*)* -> r*
2 drop empty unions: r+emptyset -> r
3 drop empty concatenations: r emptyset -> emptyset
4 factor where obvious: rs+rt -> r(s+t)
5 apply r* = epsilon + r r* when it shortens a case split
6 return the rewritten expressionThere is no canonical "simplest" RE in general (two minimal-state DFAs are unique up to renaming, but many REs can denote the same language). Distinct regular expressions can denote the same language; follow any syntax or form required by the question.
Mathematical explanation
Regular languages via expressions
A language is regular if some regular expression denotes it. Equivalent definitions (proved later): accepted by a DFA; accepted by an NFA; generated by a right-linear (Type-3) grammar.
Finite languages are regular: (each written as a concatenation of symbols). itself is when .
Why star is the only iteration needed
Concatenation and union alone generate only finite languages. Star adds unbounded repetition, which is exactly what finite-memory machines can count in unary patterns such as , but not what they need for .
Worked examples
Example 1: membership in
.
| String | In the language? | Reason |
|---|---|---|
| yes | ||
aaa | yes | |
bb | yes | |
aaabb | yes | |
ba | no | a appears before an |
aba | no | an after a |
Example 2: versus
- .
- Strings not in it include
ab,ba,aa. - , every string.
So ab distinguishes them.
Example 3: simplify
Both and equal (any string can be split into blocks of 's followed by 's, or vice versa). Their concatenation is still . A simplified expression is .
Example 4: language of strings ending with 01
One expression: .
Check: 01 matches ( then 01); 101 matches; 0 does not; 010 does not (ends with 10).
A structurally different correct expression is : cut the string just after each occurrence of 01; every block is some 1s, then at least one 0, then a 1. Proving two expressions equal this way (a decomposition argument) is an alternative to building automata.
Example 5: and traps
- .
- .
- .
- .
Important properties and observations
- Precedence: before concatenation before .
- Regular expressions denote regular languages; not every language has an RE (for example ).
- Many different REs denote the same language; minimization is for DFAs, not for REs.
- always contains .
- If , then and .
Common misconceptions.
- Reading as .
- Thinking every infinite language needs an RE with star nested two deep; one star often suffices.
- Claiming fails; union is commutative.
- Claiming always; concatenation is not commutative.
GATE examination perspective
- PRECEDENCE TRAPS. Expand carefully before comparing languages.
- WHICH STRINGS MATCH. Prefer converting a short RE to an NFA/DFA when membership of many strings is asked.
- EQUIVALENCE OF TWO RES. Convert both to minimal DFAs and compare, or argue by identities. Do not rely on looking similar.
- DESCRIBE THE LANGUAGE IN WORDS. Translate as "strings containing at least one 1".
- LEXICAL REs. Token patterns in compilers are regular expressions; longest-match and priority rules are compiler issues, not pure language equality.
- ONE COUNTEREXAMPLE KILLS AN IDENTITY. For a false claim, find the shortest string in one side and not the other; test , single symbols, then
abandba. - VERSUS . but ; is the identity for concatenation, for union.
- "AT LEAST" AND "AT MOST". "At least one " is ; "at most one" is . Check which one the wording asks for.
Summary
Sources
- J. E. Hopcroft, R. Motwani, J. D. Ullman, Introduction to Automata Theory, Languages, and Computation, 3rd ed., Pearson Addison-Wesley: Chapter 3, Sections 3.1 (regular expressions), 3.4 (algebraic laws).
- M. Sipser, Introduction to the Theory of Computation, 3rd ed., Cengage Learning: Chapter 1, Section 1.3 (regular expressions).
- P. Linz, An Introduction to Formal Languages and Automata, 6th ed., Jones & Bartlett Learning: Chapter 3, Section 3.1 (regular expressions).