Models of Computing
Regular Expressions
1
Formal models of computation
•
•
•
•
•
•
•
•
What can be computed?
What is a valid program?
What is a valid name of a variable in python?
What is the strongest possible programming
language?
What is the simplest possible computer?
Can we say something about “all the possible
algorithms”?
How much time is required (by the “best”
algorithm) for solving a given computational task?
How “complicated” is solving that task?
2
Three types of theories
• What can be computed using “simple”
computing machines or simple computing
processes?
“Automata and Formal Languages”
• What can be computed by general computers?
“Computability Theory” )"("חישוביות
• What can be computed efficiently?
“Complexity Theory” )"(סיבוכיות
3
Machines vs. Functions
Study the computing machines Study the computed functions
• “Finite State Machines”
– Finite memory
• “Push Down Automata”
– + Stack
• “Turing Machines”
– + Array-like Memory
• Regular languages
– Simple rules
• Context-Free languages
– + Simple recursion
• Computable Languages
– Anything
4
Functions that machines compute
• Mathematicians:
– Function : Natural Numbers Natural Numbers
• Computer Scientists:
– Function : Strings Strings
• Basically the same:
– A natural number can be encoded by a string: str(x)
– A string can be encoded by a number: i ord(s[i])*basei
• Theoretical Computer Scientists:
– Function : Strings Yes/No (Called a “Language”)
• Basically the same:
– Consider each bit of the answer
5
Finite Automata
• Very simply model of a machine with finite
memory.
• Input: a String. Output: accept/reject
0
start
accept
1
1
0
0
1
0 reject
1 accept
10 reject
11 reject
100 accept
…
10100 reject
10101 reject
10110 accpet
…
6
Regular Languages: Rules of the game
• Finite “Alphabet”
• Compose a “regular expression” from a finite
combination of the following ingredients:
– Empty String
– Single letter
– Concatenation
– Or (Notation: A | B)
– Repetition (Notation A*)
• The “Language” of this regular expression is the
set of Strings that fit the expression
7
Examples with Alphabet = {0, 1}
• (0 | 1) (0 | 1) (0 | 1)
– 010
• 0(00)*
– 00000
• (0 | 1)* 0
– 10110
• (00 | 11)*
– 001111110011
• (1 0 0*)* 1
– 10001000101
• (0* 1 0* 1 0*)*
– 10110001
8
More examples
Int = 0 | (+ | - | )( (1 | 2 | … | 9) (0 | 1 | ... | 9)*)
– -530
Int_List = [ | Int ( , Int)* ]
Empty string
– [34,12]
The (boy | girl | dog) (ate | lost) the (apple | pear)
– The boy ate the pear
The boy ran very* fast from the scary* lion
– The boy ran very very very fast from the lion
9
Context Free Languages
Recursive definitions:
exp: number
| ( exp )
| exp op exp
(5+2)-(1+3)
(E+E)-(E+E)
( E ) -( E )
E E
E
10
The algorithmic problem
Input: regular expression, text
Output: does the text fit the regular expression
Variants:
– Does it contain the regexp as a substring?
– Which parts match which parts?
Algorithm 1:
Recursion + Memoization
Algorithm 2:
Use Finite State Machines…
11
Regular Expressions in Python
12
Some Convenient Syntax
•
•
•
•
•
•
“[1fg]” == 1|f|g
“[a-e]” == [abcde]
“[^abc]” == any letter except a|b|c
“.” == any letter
“^” == beginning of line
“$” == end of line
13
Repetitions
•
•
•
•
•
* : 0 or more times
+ : 1 or more times
? : 0 or 1 times
{m} : Exactly m times
{m,n} : between m and n times
14
The annoying backslash
• “\” is used in python as an “escape character”, e.g.
“\n”
• “\” is used in regular expressions for various purposes
such as matching the “special characters”
• The python notation r`abc’ gives “raw” strings,
eliminating the python escapes
15
Groups
16
Parsing Phone Numbers
17
© Copyright 2026 Paperzz