Yesterday we discussed Deterministic Finite Automata. One interesting application of these mathematical objects is pattern matching, usually in the form of Regular Expressions. \\
(abbreviated \say{regex}, which is pronounced like \say{gif})
\vspace{2mm}
Regex is a language used to specify patterns in a string. You can think it as a concise way to define a DFA, using text instead of a huge graph. \\
Often enough, a clever regex pattern can do the work of a few hundred lines of code. \\
\vspace{2mm}
Like the DFAs we have studied, a regex pattern \textit{accepts} or \textit{rejects} a string. However, we don't usually use this terminology when discussing regex, instead opting to say a pattern \textit{matches} or \textit{doesn't match} a string. \\
\vspace{5mm}
\textbf{Quantifiers}\\
Quantifiers tell us how many of a character to match. \\
There are four of them:
\htexttt{+}, \htexttt{*}, \htexttt{?}, and \htexttt{\{\}}
\vspace{2mm}
\htexttt{+} means \say{match one or more of the preceding token}\\
\htexttt{*} means \say{match zero or more of the preceding token}\\
For example, the pattern \htexttt{ca+t} will match the following strings:
\begin{itemize}
\item\texttt{cat}
\item\texttt{caat}
\item\texttt{caaaaaaaat}
\end{itemize}
\htexttt{ca+t} will \textbf{not} match the string \texttt{ct}. \\
The pattern \htexttt{ca*t} will match all the strings above, including \texttt{ct}.
\vspace{2mm}
\htexttt{?} means \say{match one or none of the preceeding token}\\
The pattern \htexttt{linea?r} will match only \texttt{linear} and \texttt{liner}\\
If you'd like to know more, check out \texttt{regexr.com}. It offers an interative regex prompt, as well as a cheatsheet that explains every other regex token there is. You can find a nice set of challenges at \texttt{http://regex.alf.nu}. \\