author | Christian Urban <christian dot urban at kcl dot ac dot uk> |
Sun, 14 Sep 2014 23:32:22 +0100 | |
changeset 245 | a5fade10c207 |
parent 244 | 771042ac7c3f |
child 248 | ce767ca23244 |
permissions | -rw-r--r-- |
105
397ecdafefd8
added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
1 |
\documentclass{article} |
237
370c0647a9bf
more material
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
217
diff
changeset
|
2 |
\usepackage{../style} |
217
cd6066f1056a
updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
123
diff
changeset
|
3 |
\usepackage{../langs} |
242
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
4 |
\usepackage{../graphics} |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
5 |
\usepackage{../data} |
108
52ee218151f9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
107
diff
changeset
|
6 |
|
112
95ee5cc5c05d
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
111
diff
changeset
|
7 |
|
105
397ecdafefd8
added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
8 |
\begin{document} |
397ecdafefd8
added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
9 |
|
397ecdafefd8
added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
10 |
\section*{Handout 1} |
397ecdafefd8
added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
11 |
|
242
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
12 |
This module is about text processing, be it for web-crawlers, |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
13 |
compilers, dictionaries, DNA-data and so on. When looking for |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
14 |
a particular string in a large text we can use the |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
15 |
Knuth-Morris-Pratt algorithm, which is currently the most |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
16 |
efficient general string search algorithm. But often we do |
243
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
17 |
\emph{not} just look for a particular string, but for string |
242
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
18 |
patterns. For example in programming code we need to identify |
243
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
19 |
what are the keywords, what are the identifiers etc. A pattern |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
20 |
for identifiers could be that they start with a letter, |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
21 |
followed by zero or more letters, numbers and the underscore. |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
22 |
Also often we face the problem that we are given a string (for |
242
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
23 |
example some user input) and want to know whether it matches a |
243
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
24 |
particular pattern. In this way we can exclude user input that |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
25 |
would otherwise have nasty effects on our program (crashing it |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
26 |
or going into an infinite loop, if not worse). \defn{Regular |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
27 |
expressions} help with conveniently specifying such patterns. |
242
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
28 |
The idea behind regular expressions is that they are a simple |
243
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
29 |
method for describing languages (or sets of strings)\ldots at |
242
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
30 |
least languages we are interested in in computer science. For |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
31 |
example there is no convenient regular expression for |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
32 |
describing the English language short of enumerating all |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
33 |
English words. But they seem useful for describing for example |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
34 |
email addresses.\footnote{See ``8 Regular Expressions You |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
35 |
Should Know'' \url{http://goo.gl/5LoVX7}} Consider the |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
36 |
following regular expression |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
37 |
|
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
38 |
\begin{equation}\label{email} |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
39 |
\texttt{[a-z0-9\_.-]+ @ [a-z0-9.-]+ . [a-z.]\{2,6\}} |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
40 |
\end{equation} |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
41 |
|
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
42 |
\noindent where the first part matches one or more lowercase |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
43 |
letters (\pcode{a-z}), digits (\pcode{0-9}), underscores, dots |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
44 |
or hyphens. The \pcode{+} ensures the ``one or more''. Then |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
45 |
comes the \pcode{@}-sign, followed by the domain name which |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
46 |
must be one or more lowercase letters, digits, underscores, |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
47 |
dots or hyphens. Note there cannot be an underscore in the |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
48 |
domain name. Finally there must be a dot followed by the |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
49 |
toplevel domain. This toplevel domain must be 2 to 6 lowercase |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
50 |
letters including the dot. Example strings which follow this |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
51 |
pattern are: |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
52 |
|
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
53 |
\begin{lstlisting}[language={},numbers=none,keywordstyle=\color{black}] |
243
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
54 |
niceandsimple@example.org |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
55 |
very.common@example.co.uk |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
56 |
a.little.lengthy.but.fine@dept.example.ac.uk |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
57 |
other.email-with-dash@example.edu |
242
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
58 |
\end{lstlisting} |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
59 |
|
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
60 |
|
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
61 |
\noindent |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
62 |
But for example the following two do not: |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
63 |
|
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
64 |
\begin{lstlisting}[language={},numbers=none,keywordstyle=\color{black}] |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
65 |
user@localserver |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
66 |
disposable.style.email.with+symbol@example.com |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
67 |
\end{lstlisting} |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
68 |
|
243
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
69 |
Identifiers, or variables, in program text are often required |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
70 |
to satisfy the constraints that they start with a letter and |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
71 |
then can be followed by zero or more letters or numbers and |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
72 |
also can include underscores, but not as the first character. |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
73 |
Such identifiers can be recognised with the regular expression |
242
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
74 |
|
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
75 |
\begin{center} |
243
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
76 |
\pcode{[a-zA-Z] [a-zA-Z0-9_]*} |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
77 |
\end{center} |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
78 |
|
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
79 |
\noindent Possible identifiers that match this regular expression |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
80 |
are \pcode{x}, \pcode{foo}, \pcode{foo_bar_1}, \pcode{A_very_42_long_object_name}, |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
81 |
but not \pcode{_i} and also not \pcode{4you}. |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
82 |
|
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
83 |
Many programming language offer libraries that can be used to |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
84 |
validate such strings against regular expressions. Also there |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
85 |
are some common, and I am sure very familiar, ways how to |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
86 |
construct regular expressions. For example in Scala we have: |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
87 |
|
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
88 |
\begin{center} |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
89 |
\begin{tabular}{lp{9cm}} |
242
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
90 |
\pcode{re*} & matches 0 or more occurrences of preceding |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
91 |
expression\\ |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
92 |
\pcode{re+} & matches 1 or more occurrences of preceding |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
93 |
expression\\ |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
94 |
\pcode{re?} & matches 0 or 1 occurrence of preceding |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
95 |
expression\\ |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
96 |
\pcode{re\{n\}} & matches exactly \pcode{n} number of |
243
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
97 |
occurrences of preceding expression\\ |
242
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
98 |
\pcode{re\{n,m\}} & matches at least \pcode{n} and at most {\tt m} |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
99 |
occurences of the preceding expression\\ |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
100 |
\pcode{[...]} & matches any single character inside the |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
101 |
brackets\\ |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
102 |
\pcode{[^...]} & matches any single character not inside the |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
103 |
brackets\\ |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
104 |
\pcode{..-..} & character ranges\\ |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
105 |
\pcode{\\d} & matches digits; equivalent to \pcode{[0-9]} |
243
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
106 |
\end{tabular} |
242
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
107 |
\end{center} |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
108 |
|
243
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
109 |
\noindent With this table you can figure out the purpose of |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
110 |
the regular expressions in the web-crawlers shown Figures |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
111 |
\ref{crawler1}, \ref{crawler2} and \ref{crawler3}. Note, |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
112 |
however, the regular expression for http-addresses in |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
113 |
web-pages is meant to be |
242
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
114 |
|
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
115 |
\[ |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
116 |
\pcode{"https?://[^"]*"} |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
117 |
\] |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
118 |
|
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
119 |
\noindent It specifies that web-addresses need to start with a |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
120 |
double quote, then comes \texttt{http} followed by an optional |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
121 |
\texttt{s} and so on. Usually we would have to escape the |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
122 |
double quotes in order to make sure we interpret the double |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
123 |
quote as character, not as double quote for a string. But |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
124 |
Scala's trick with triple quotes allows us to omit this kind |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
125 |
of escaping. As a result we can just write: |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
126 |
|
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
127 |
\[ |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
128 |
\pcode{""""https?://[^"]*"""".r} |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
129 |
\] |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
130 |
|
243
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
131 |
\noindent Note also that the convention in Scala is that |
242
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
132 |
\texttt{.r} converts a string into a regular expression. I |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
133 |
leave it to you to ponder whether this regular expression |
243
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
134 |
really captures all possible web-addresses. |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
135 |
|
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
136 |
\subsection*{Why Study Regular Expressions?} |
242
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
137 |
|
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
138 |
Regular expressions were introduced by Kleene in the 1950ies |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
139 |
and they have been object of intense study since then. They |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
140 |
are nowadays pretty much ubiquitous in computer science. I am |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
141 |
sure you have come across them before. Why on earth then is |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
142 |
there any interest in studying them again in depth in this |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
143 |
module? Well, one answer is in the following graph about |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
144 |
regular expression matching in Python and in Ruby. |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
145 |
|
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
146 |
\begin{center} |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
147 |
\begin{tikzpicture}[y=.1cm, x=.15cm] |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
148 |
%axis |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
149 |
\draw (0,0) -- coordinate (x axis mid) (30,0); |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
150 |
\draw (0,0) -- coordinate (y axis mid) (0,30); |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
151 |
%ticks |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
152 |
\foreach \x in {0,5,...,30} |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
153 |
\draw (\x,1pt) -- (\x,-3pt) node[anchor=north] {\x}; |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
154 |
\foreach \y in {0,5,...,30} |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
155 |
\draw (1pt,\y) -- (-3pt,\y) node[anchor=east] {\y}; |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
156 |
%labels |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
157 |
\node[below=0.6cm] at (x axis mid) {number of \texttt{a}s}; |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
158 |
\node[rotate=90,above=0.8cm] at (y axis mid) {time in secs}; |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
159 |
%plots |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
160 |
\draw[color=blue] plot[mark=*] |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
161 |
file {re-python.data}; |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
162 |
\draw[color=brown] plot[mark=triangle*] |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
163 |
file {re-ruby.data}; |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
164 |
%legend |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
165 |
\begin{scope}[shift={(4,20)}] |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
166 |
\draw[color=blue] (0,0) -- |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
167 |
plot[mark=*] (0.25,0) -- (0.5,0) |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
168 |
node[right]{\small Python}; |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
169 |
\draw[yshift=-\baselineskip, color=brown] (0,0) -- |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
170 |
plot[mark=triangle*] (0.25,0) -- (0.5,0) |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
171 |
node[right]{\small Ruby}; |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
172 |
\end{scope} |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
173 |
\end{tikzpicture} |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
174 |
\end{center} |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
175 |
|
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
176 |
\noindent This graph shows that Python needs approximately 29 |
243
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
177 |
seconds for finding out whether a string of 28 \texttt{a}s |
242
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
178 |
matches the regular expression \texttt{[a?]\{28\}[a]\{28\}}. |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
179 |
Ruby is even slightly worse.\footnote{In this example Ruby |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
180 |
uses the slightly different regular expression |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
181 |
\texttt{a?a?a?...a?a?aaa...aa}, where the \texttt{a?} and |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
182 |
\texttt{a} each occur $n$ times.} Admittedly, this regular |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
183 |
expression is carefully chosen to exhibit this exponential |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
184 |
behaviour, but similar ones occur more often than one wants in |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
185 |
``real life''. They are sometimes called \emph{evil regular |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
186 |
expressions} because they have the potential to make regular |
243
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
187 |
expression matching engines to topple over, like in Python and |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
188 |
Ruby. The problem with evil regular expressions is that they |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
189 |
can have some serious consequences, for example, if you use |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
190 |
them in your web-application. The reason is that hackers can |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
191 |
look for these instances where the matching engine behaves |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
192 |
badly and then mount a nice DoS-attack against your |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
193 |
application. |
242
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
194 |
|
243
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
195 |
It will be instructive to look behind the ``scenes'' to find |
242
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
196 |
out why Python and Ruby (and others) behave so badly when |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
197 |
matching with evil regular expressions. But we will also look |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
198 |
at a relatively simple algorithm that solves this problem much |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
199 |
better than Python and Ruby do\ldots actually it will be two |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
200 |
versions of the algorithm: the first one will be able to |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
201 |
process strings of approximately 1,000 \texttt{a}s in 30 |
243
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
202 |
seconds, while the second version will even be able to process |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
203 |
up to 12,000 in less than 10(!) seconds, see the graph below: |
242
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
204 |
|
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
205 |
\begin{center} |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
206 |
\begin{tikzpicture}[y=.1cm, x=.0006cm] |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
207 |
%axis |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
208 |
\draw (0,0) -- coordinate (x axis mid) (12000,0); |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
209 |
\draw (0,0) -- coordinate (y axis mid) (0,30); |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
210 |
%ticks |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
211 |
\foreach \x in {0,2000,...,12000} |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
212 |
\draw (\x,1pt) -- (\x,-3pt) node[anchor=north] {\x}; |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
213 |
\foreach \y in {0,5,...,30} |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
214 |
\draw (1pt,\y) -- (-3pt,\y) node[anchor=east] {\y}; |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
215 |
%labels |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
216 |
\node[below=0.6cm] at (x axis mid) {number of \texttt{a}s}; |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
217 |
\node[rotate=90, above=0.8cm] at (y axis mid) {time in secs}; |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
218 |
|
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
219 |
%plots |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
220 |
\draw[color=green] plot[mark=square*, mark options={fill=white} ] |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
221 |
file {re2b.data}; |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
222 |
\draw[color=black] plot[mark=square*, mark options={fill=white} ] |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
223 |
file {re3.data}; |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
224 |
\end{tikzpicture} |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
225 |
\end{center} |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
226 |
|
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
227 |
\subsection*{Basic Regular Expressions} |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
228 |
|
243
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
229 |
The regular expressions shown above, for example for Scala, we |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
230 |
will call \emph{extended regular expressions}. The ones we |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
231 |
will mainly study in this module are \emph{basic regular |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
232 |
expressions}, which by convention we will just call |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
233 |
\emph{regular expressions}, if it is clear what we mean. The |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
234 |
attraction of (basic) regular expressions is that many |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
235 |
features of the extended ones are just syntactic sugar. |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
236 |
(Basic) regular expressions are defined by the following |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
237 |
grammar: |
242
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
238 |
|
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
239 |
\begin{center} |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
240 |
\begin{tabular}{r@{\hspace{1mm}}r@{\hspace{1mm}}l@{\hspace{13mm}}l} |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
241 |
$r$ & $::=$ & $\varnothing$ & null\\ |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
242 |
& $\mid$ & $\epsilon$ & empty string / "" / []\\ |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
243 |
& $\mid$ & $c$ & single character\\ |
243
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
244 |
& $\mid$ & $r_1 + r_2$ & alternative / choice\\ |
242
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
245 |
& $\mid$ & $r_1 \cdot r_2$ & sequence\\ |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
246 |
& $\mid$ & $r^*$ & star (zero or more)\\ |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
247 |
\end{tabular} |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
248 |
\end{center} |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
249 |
|
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
250 |
\noindent Because we overload our notation, there are some |
243
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
251 |
subtleties you should be aware of. When regular expressions |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
252 |
are referred to then $\varnothing$ does not stand for the |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
253 |
empty set: rather it is a particular pattern that does not |
242
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
254 |
match any string. Similarly, in the context of regular |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
255 |
expressions, $\epsilon$ does not stand for the empty string |
243
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
256 |
(as in many places in the literature) but for a regular |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
257 |
expression that matches the empty string. The letter $c$ |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
258 |
stands for any character from the alphabet at hand. Again in |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
259 |
the context of regular expressions, it is a particular pattern |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
260 |
that can match the specified character. You should also be |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
261 |
careful with our overloading of the star: assuming you have |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
262 |
read the handout about our basic mathematical notation, you |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
263 |
will see that in the context of languages (sets of strings) |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
264 |
the star stands for an operation on languages. While here |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
265 |
$r^*$ stands for a regular expression, which is different from |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
266 |
the operation on sets is defined as |
242
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
267 |
|
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
268 |
\[ |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
269 |
A^* \dn \bigcup_{0\le n} A^n |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
270 |
\] |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
271 |
|
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
272 |
We will use parentheses to disambiguate regular expressions. |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
273 |
Parentheses are not really part of a regular expression, and |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
274 |
indeed we do not need them in our code because there the tree |
243
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
275 |
structure of regular expressions is always clear. But for |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
276 |
writing them down in a more mathematical fashion, parentheses |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
277 |
will be helpful. For example we will write $(r_1 + r_2)^*$, |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
278 |
which is different from, say $r_1 + (r_2)^*$. The former means |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
279 |
roughly zero or more times $r_1$ or $r_2$, while the latter |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
280 |
means $r_1$ or zero or more times $r_2$. This will turn out |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
281 |
are two different patterns, which match in general different |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
282 |
strings. We should also write $(r_1 + r_2) + r_3$, which is |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
283 |
different from the regular expression $r_1 + (r_2 + r_3)$, but |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
284 |
in case of $+$ and $\cdot$ we actually do not care about the |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
285 |
order and just write $r_1 + r_2 + r_3$, or $r_1 \cdot r_2 |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
286 |
\cdot r_3$, respectively. The reasons for this will become |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
287 |
clear shortly. In the literature you will often find that the |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
288 |
choice $r_1 + r_2$ is written as $r_1\mid{}r_2$ or |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
289 |
$r_1\mid\mid{}r_2$. Also following the convention in the |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
290 |
literature, we will often omit the $\cdot$ all together. This |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
291 |
is to make some concrete regular expressions more readable. |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
292 |
For example the regular expression for email addresses shown |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
293 |
in \eqref{email} would look like |
242
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
294 |
|
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
295 |
\[ |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
296 |
\texttt{[...]+} \;\cdot\; \texttt{@} \;\cdot\; |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
297 |
\texttt{[...]+} \;\cdot\; \texttt{.} \;\cdot\; |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
298 |
\texttt{[...]\{2,6\}} |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
299 |
\] |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
300 |
|
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
301 |
\noindent |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
302 |
which is much less readable than \eqref{email}. Similarly for |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
303 |
the regular expression that matches the string $hello$ we |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
304 |
should write |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
305 |
|
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
306 |
\[ |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
307 |
{\it h} \cdot {\it e} \cdot {\it l} \cdot {\it l} \cdot {\it o} |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
308 |
\] |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
309 |
|
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
310 |
\noindent |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
311 |
but often just write {\it hello}. |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
312 |
|
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
313 |
If you prefer to think in terms of the implementation |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
314 |
of regular expressions in Scala, the constructors and |
245
a5fade10c207
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
244
diff
changeset
|
315 |
classes relate as follows\footnote{More about Scala is |
a5fade10c207
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
244
diff
changeset
|
316 |
in the handout about a crash-course on Scala.} |
242
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
317 |
|
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
318 |
\begin{center} |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
319 |
\begin{tabular}{rcl} |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
320 |
$\varnothing$ & $\mapsto$ & \texttt{NULL}\\ |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
321 |
$\epsilon$ & $\mapsto$ & \texttt{EMPTY}\\ |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
322 |
$c$ & $\mapsto$ & \texttt{CHAR(c)}\\ |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
323 |
$r_1 + r_2$ & $\mapsto$ & \texttt{ALT(r1, r2)}\\ |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
324 |
$r_1 \cdot r_2$ & $\mapsto$ & \texttt{SEQ(r1, r2)}\\ |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
325 |
$r^*$ & $\mapsto$ & \texttt{STAR(r)} |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
326 |
\end{tabular} |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
327 |
\end{center} |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
328 |
|
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
329 |
A source of confusion might arise from the fact that we |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
330 |
use the term \emph{basic regular expression} for the regular |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
331 |
expressions used in ``theory'' and defined above, and |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
332 |
\emph{extended regular expression} for the ones used in |
243
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
333 |
``practice'', for example in Scala. If runtime is not an |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
334 |
issue, then the latter can be seen as syntactic sugar of |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
335 |
the former. For example we could replace |
242
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
336 |
|
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
337 |
\begin{center} |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
338 |
\begin{tabular}{rcl} |
243
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
339 |
$r+$ & $\mapsto$ & $r\cdot r^*$\\ |
242
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
340 |
$r?$ & $\mapsto$ & $\epsilon + r$\\ |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
341 |
$\backslash d$ & $\mapsto$ & $0 + 1 + 2 + \ldots + 9$\\ |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
342 |
$[\text{\it a - z}]$ & $\mapsto$ & $a + b + \ldots + z$\\ |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
343 |
\end{tabular} |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
344 |
\end{center} |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
345 |
|
243
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
346 |
|
242
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
347 |
\subsection*{The Meaning of Regular Expressions} |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
348 |
|
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
349 |
So far we have only considered informally what the |
243
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
350 |
\emph{meaning} of a regular expression is. This is not good |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
351 |
enough for specifications of what algorithms are supposed to |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
352 |
do or which problems they are supposed to solve. |
242
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
353 |
|
243
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
354 |
To define the meaning of a regular expression we will |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
355 |
associate with every regular expression a language, or set of |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
356 |
strings. This language contains all the strings the regular |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
357 |
expression is supposed to match. To understand what is going |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
358 |
on here it is crucial that you have read the handout |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
359 |
about basic mathematical notations. |
242
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
360 |
|
243
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
361 |
The \defn{meaning of a regular expression} can be defined |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
362 |
by a recursive function called $L$ (for language), which |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
363 |
is defined as follows |
242
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
364 |
|
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
365 |
\begin{center} |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
366 |
\begin{tabular}{rcl} |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
367 |
$L(\varnothing)$ & $\dn$ & $\varnothing$\\ |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
368 |
$L(\epsilon)$ & $\dn$ & $\{[]\}$\\ |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
369 |
$L(c)$ & $\dn$ & $\{"c"\}$\\ |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
370 |
$L(r_1+ r_2)$ & $\dn$ & $L(r_1) \cup L(r_2)$\\ |
243
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
371 |
$L(r_1 \cdot r_2)$ & $\dn$ & $L(r_1) \,@\, L(r_2)$\\ |
242
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
372 |
$L(r^*)$ & $\dn$ & $(L(r))^*$\\ |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
373 |
\end{tabular} |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
374 |
\end{center} |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
375 |
|
243
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
376 |
\noindent As a result we can now precisely state what the |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
377 |
meaning, for example, of the regular expression $h \cdot |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
378 |
e \cdot l \cdot l \cdot o$ is, namely |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
379 |
|
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
380 |
\[ |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
381 |
L({\it h} \cdot {\it e} \cdot {\it l} \cdot {\it l} \cdot |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
382 |
{\it o}) = \{"hello"\} |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
383 |
\] |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
384 |
|
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
385 |
\noindent This is expected because this regular expression |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
386 |
is only supposed to match the ``$hello$''-string. Similarly if |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
387 |
we have the choice-regular-expression $a + b$, its meaning is |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
388 |
|
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
389 |
\[ |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
390 |
L(a + b) = \{"a", "b"\} |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
391 |
\] |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
392 |
|
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
393 |
\noindent You can now also see why we do not make a difference |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
394 |
between the different regular expressions $(r_1 + r_2) + r_3$ |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
395 |
and $r_1 + (r_2 + r_3)$\ldots they are not the same regular |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
396 |
expression, but have the same meaning. |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
397 |
|
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
398 |
\begin{eqnarray*} |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
399 |
L((r_1 + r_2) + r_3) & = & L(r_1 + r_2) \cup L(r_3)\\ |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
400 |
& = & L(r_1) \cup L(r_2) \cup L(r_3)\\ |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
401 |
& = & L(r_1) \cup L(r_2 + r_3)\\ |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
402 |
& = & L(r_1 + (r_2 + r_3)) |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
403 |
\end{eqnarray*} |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
404 |
|
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
405 |
The point of the definition of $L$ is that we can use it to |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
406 |
precisely specify when a string $s$ is matched by a regular |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
407 |
expression $r$, namely if and only if $s \in L(r)$. In fact we |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
408 |
will write a program \pcode{match} that takes any string $s$ |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
409 |
and any regular expression $r$ as argument and returns |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
410 |
\emph{yes}, if $s \in L(r)$ and \emph{no}, if $s \not\in |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
411 |
L(r)$. We leave this for the next lecture. |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
412 |
|
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
413 |
There is one more feature of regular expressions that is worth |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
414 |
mentioning. Given some strings, there are in general many |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
415 |
different regular expressions that can recognise these |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
416 |
strings. This is obvious with the regular expression $a + b$ |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
417 |
which can match the strings $a$ and $b$. But also the regular |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
418 |
expression $b + a$ would match the same strings. However, |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
419 |
sometimes it is not so obvious whether two regular expressions |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
420 |
match the same strings: for example do $r^*$ and $\epsilon + r |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
421 |
\cdot r^*$ match the same strings? What about $\varnothing^*$ |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
422 |
and $\epsilon^*$? This suggests the following relation between |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
423 |
\defn{equivalent regular expressions}: |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
424 |
|
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
425 |
\[ |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
426 |
r_1 \equiv r_2 \;\dn\; L(r_1) = L(r_2) |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
427 |
\] |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
428 |
|
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
429 |
\noindent That means two regular expressions are equivalent if |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
430 |
they match the same set of strings. Therefore we do not really |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
431 |
distinguish between the different regular expression $(r_1 + |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
432 |
r_2) + r_3$ and $r_1 + (r_2 + r_3)$, because they are |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
433 |
equivalent. I leave you to the question whether |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
434 |
|
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
435 |
\[ |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
436 |
\varnothing^* \equiv \epsilon^* |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
437 |
\] |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
438 |
|
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
439 |
\noindent holds. Such equivalences will be important for out |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
440 |
matching algorithm, because we can use them to simplify |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
441 |
regular expressions. |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
442 |
|
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
443 |
\subsection*{My Fascination for Regular Expressions} |
242
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
444 |
|
243
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
445 |
Up until a few years ago I was not really interested in |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
446 |
regular expressions. They have been studied for the last 60 |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
447 |
years (by smarter people than me)---surely nothing new can be |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
448 |
found out about them. I even have the vague recollection that |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
449 |
I did not quite understand them during my study. If I remember |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
450 |
correctly,\footnote{That was really a long time ago.} I got |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
451 |
utterly confused about $\epsilon$ and the empty |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
452 |
string.\footnote{Obviously the lecturer must have been bad.} |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
453 |
Since my study, I have used regular expressions for |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
454 |
implementing lexers and parsers as I have always been |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
455 |
interested in all kinds of programming languages and |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
456 |
compilers, which invariably need regular expression in some |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
457 |
form or shape. |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
458 |
|
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
459 |
To understand my fascination nowadays with regular |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
460 |
expressions, you need to know that my main scientific interest |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
461 |
for the last 14 years has been with in theorem provers. I am a |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
462 |
core developer of one of |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
463 |
them.\footnote{\url{http://isabelle.in.tum.de}} Theorem |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
464 |
provers are systems in which you can formally reason about |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
465 |
mathematical concepts, but also about programs. In this way |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
466 |
they can help with writing bug-free code. Theorem provers have |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
467 |
proved already their value in a number of systems (even in |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
468 |
terms of hard cash), but they are still clunky and difficult |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
469 |
to use by average programmers. |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
470 |
|
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
471 |
Anyway, in about 2011 I came across the notion of |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
472 |
\defn{derivatives of regular expressions}. This notion allows |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
473 |
one to do almost all calculations in regular language theory |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
474 |
on the level of regular expressions, not needing any automata. |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
475 |
This is important because automata are graphs and it is rather |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
476 |
difficult to reason about graphs in theorem provers. In |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
477 |
contrast, to reason about regular expressions is easy-peasy in |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
478 |
theorem provers. Is this important? I think yes, because |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
479 |
according to Kuklewicz nearly all POSIX-based regular |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
480 |
expression matchers are |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
481 |
buggy.\footnote{\url{http://www.haskell.org/haskellwiki/Regex_Posix}} |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
482 |
With my PhD student Fahad Ausaf I am currently working on |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
483 |
proving the correctness for one such algorithm that was |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
484 |
proposed by Sulzmann and Lu in |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
485 |
2014.\footnote{\url{http://goo.gl/bz0eHp}} This would be an |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
486 |
attractive results since we will be able to prove that the |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
487 |
algorithm is really correct, but also that the machine code(!) |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
488 |
that implements this code efficiently is correct. Writing |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
489 |
programs in this way does not leave any room for potential |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
490 |
errors or bugs. How nice! |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
491 |
|
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
492 |
What also helped with my fascination with regular expressions |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
493 |
is that we could indeed find out new things about them that |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
494 |
have surprised some experts in the field of regular |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
495 |
expressions. Together with two colleagues from China, I was |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
496 |
able to prove the Myhill-Nerode theorem by only using regular |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
497 |
expressions and the notion of derivatives. Earlier versions of |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
498 |
this theorem used always automata in the proof. Using this |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
499 |
theorem we can show that regular languages are closed under |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
500 |
complementation, something which Gasarch in his |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
501 |
blog\footnote{\url{http://goo.gl/2R11Fw}} assumed can only be |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
502 |
shown via automata. Even sombody who has written a 700+-page |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
503 |
book\footnote{\url{http://goo.gl/fD0eHx}} on regular |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
504 |
exprssions did not know better. Well, we showed it can also be |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
505 |
done with regular expressions only. What a feeling if you |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
506 |
are an outsider to the subject! |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
507 |
|
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
508 |
To conclude: Despite my early ignorance about regular |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
509 |
expressions, I find them now quite interesting. They have a |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
510 |
beautiful mathematical theory behind them. They have practical |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
511 |
importance (remember the shocking runtime of the regular |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
512 |
expression matchers in Python and Ruby in some instances). |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
513 |
People who are not very familiar with the mathematical |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
514 |
background get them consistently wrong. The hope is that we |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
515 |
can do better in the future---for example by proving that the |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
516 |
algorithms actually satisfy their specification and that the |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
517 |
corresponding implementations do not contain any bugs. We are |
8d5aaf5b0031
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
518 |
close, but not yet quite there. |
242
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
519 |
|
244
771042ac7c3f
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
243
diff
changeset
|
520 |
Despite my fascination, I am also happy to admit that regular |
771042ac7c3f
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
243
diff
changeset
|
521 |
expressions have their shortcomings. There are some well-known |
771042ac7c3f
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
243
diff
changeset
|
522 |
``theoretical shortcomings'', for example recognising strings |
771042ac7c3f
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
243
diff
changeset
|
523 |
of the form $a^{n}b^{n}$. I am not so bothered by them. What I |
771042ac7c3f
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
243
diff
changeset
|
524 |
am bothered about is when regular expressions are in the way |
771042ac7c3f
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
243
diff
changeset
|
525 |
of practical programming. For example, it turns out that the |
771042ac7c3f
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
243
diff
changeset
|
526 |
regular expression for email addresses shown in \eqref{email} |
771042ac7c3f
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
243
diff
changeset
|
527 |
is hopelessly inadequate for recognising all of them (despite |
771042ac7c3f
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
243
diff
changeset
|
528 |
being touted as something every computer scientist should know |
771042ac7c3f
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
243
diff
changeset
|
529 |
about). The W3 Consortium (which standardises the Web) |
771042ac7c3f
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
243
diff
changeset
|
530 |
proposes to use the following, already more complicated |
771042ac7c3f
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
243
diff
changeset
|
531 |
regular expressions: |
771042ac7c3f
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
243
diff
changeset
|
532 |
|
771042ac7c3f
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
243
diff
changeset
|
533 |
{\small\begin{lstlisting}[language={},keywordstyle=\color{black},numbers=none] |
771042ac7c3f
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
243
diff
changeset
|
534 |
[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)* |
771042ac7c3f
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
243
diff
changeset
|
535 |
\end{lstlisting}} |
771042ac7c3f
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
243
diff
changeset
|
536 |
|
771042ac7c3f
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
243
diff
changeset
|
537 |
\noindent But they admit that by using this regular expression |
771042ac7c3f
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
243
diff
changeset
|
538 |
they wilfully violate the RFC 5322 standard which specifies |
771042ac7c3f
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
243
diff
changeset
|
539 |
the syntax of email addresses. With their proposed regular |
771042ac7c3f
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
243
diff
changeset
|
540 |
expression they are too strict in some cases and too lax in |
771042ac7c3f
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
243
diff
changeset
|
541 |
others. Not a good situation to be in. A regular expression |
771042ac7c3f
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
243
diff
changeset
|
542 |
that is claimed to be closer to the standard is shown in |
771042ac7c3f
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
243
diff
changeset
|
543 |
Figure~\ref{monster}. Whether this claim is true or not, I |
771042ac7c3f
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
243
diff
changeset
|
544 |
would not know---the only thing I can say it is a monstrosity. |
771042ac7c3f
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
243
diff
changeset
|
545 |
However, this might actually be an argument against the |
771042ac7c3f
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
243
diff
changeset
|
546 |
standard, rather than against regular expressions. Still it |
771042ac7c3f
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
243
diff
changeset
|
547 |
is good to know that some tasks in text processing just |
771042ac7c3f
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
243
diff
changeset
|
548 |
cannot be achieved by using regular expressions. |
771042ac7c3f
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
243
diff
changeset
|
549 |
|
771042ac7c3f
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
243
diff
changeset
|
550 |
|
242
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
551 |
\begin{figure}[p] |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
552 |
\lstinputlisting{../progs/crawler1.scala} |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
553 |
\caption{The Scala code for a simple web-crawler that checks |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
554 |
for broken links in a web-page. It uses the regular expression |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
555 |
\texttt{http\_pattern} in Line~15 for recognising URL-addresses. |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
556 |
It finds all links using the library function |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
557 |
\texttt{findAllIn} in Line~21.\label{crawler1}} |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
558 |
\end{figure} |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
559 |
|
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
560 |
|
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
561 |
\begin{figure}[p] |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
562 |
\lstinputlisting{../progs/crawler2.scala} |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
563 |
|
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
564 |
\caption{A version of the web-crawler that only follows links |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
565 |
in ``my'' domain---since these are the ones I am interested in |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
566 |
to fix. It uses the regular expression \texttt{my\_urls} in |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
567 |
Line~16 to check for my name in the links. The main change is |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
568 |
in Lines~26--29 where there is a test whether URL is in ``my'' |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
569 |
domain or not.\label{crawler2}} |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
570 |
|
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
571 |
\end{figure} |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
572 |
|
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
573 |
\begin{figure}[p] |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
574 |
\lstinputlisting{../progs/crawler3.scala} |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
575 |
|
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
576 |
\caption{A small email harvester---whenever we download a |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
577 |
web-page, we also check whether it contains any email |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
578 |
addresses. For this we use the regular expression |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
579 |
\texttt{email\_pattern} in Line~16. The main change is in Line |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
580 |
32 where all email addresses that can be found in a page are |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
581 |
printed.\label{crawler3}} |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
582 |
\end{figure} |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
583 |
|
244
771042ac7c3f
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
243
diff
changeset
|
584 |
\begin{figure}[p] |
771042ac7c3f
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
243
diff
changeset
|
585 |
\tiny |
771042ac7c3f
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
243
diff
changeset
|
586 |
\begin{center} |
771042ac7c3f
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
243
diff
changeset
|
587 |
\begin{minipage}{0.8\textwidth} |
771042ac7c3f
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
243
diff
changeset
|
588 |
\lstinputlisting[language={},keywordstyle=\color{black},numbers=none]{../progs/email-rexp} |
771042ac7c3f
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
243
diff
changeset
|
589 |
\end{minipage} |
771042ac7c3f
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
243
diff
changeset
|
590 |
\end{center} |
771042ac7c3f
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
243
diff
changeset
|
591 |
|
771042ac7c3f
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
243
diff
changeset
|
592 |
\caption{Nothing that can be said\ldots\label{monster}} |
771042ac7c3f
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
243
diff
changeset
|
593 |
\end{figure} |
108
52ee218151f9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
107
diff
changeset
|
594 |
|
112
95ee5cc5c05d
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
111
diff
changeset
|
595 |
|
105
397ecdafefd8
added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
596 |
\end{document} |
397ecdafefd8
added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
597 |
|
397ecdafefd8
added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
598 |
%%% Local Variables: |
397ecdafefd8
added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
599 |
%%% mode: latex |
397ecdafefd8
added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
600 |
%%% TeX-master: t |
397ecdafefd8
added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
601 |
%%% End: |