\documentclass[10pt,a4paper]{article} % Packages \usepackage{fancyhdr} % For header and footer \usepackage{multicol} % Allows multicols in tables \usepackage{tabularx} % Intelligent column widths \usepackage{tabulary} % Used in header and footer \usepackage{hhline} % Border under tables \usepackage{graphicx} % For images \usepackage{xcolor} % For hex colours %\usepackage[utf8x]{inputenc} % For unicode character support \usepackage[T1]{fontenc} % Without this we get weird character replacements \usepackage{colortbl} % For coloured tables \usepackage{setspace} % For line height \usepackage{lastpage} % Needed for total page number \usepackage{seqsplit} % Splits long words. %\usepackage{opensans} % Can't make this work so far. Shame. Would be lovely. \usepackage[normalem]{ulem} % For underlining links % Most of the following are not required for the majority % of cheat sheets but are needed for some symbol support. \usepackage{amsmath} % Symbols \usepackage{MnSymbol} % Symbols \usepackage{wasysym} % Symbols %\usepackage[english,german,french,spanish,italian]{babel} % Languages % Document Info \author{Nima (nimakarimian)} \pdfinfo{ /Title (python-regex-regular-expression.pdf) /Creator (Cheatography) /Author (Nima (nimakarimian)) /Subject (python regex(regular expression) Cheat Sheet) } % Lengths and widths \addtolength{\textwidth}{6cm} \addtolength{\textheight}{-1cm} \addtolength{\hoffset}{-3cm} \addtolength{\voffset}{-2cm} \setlength{\tabcolsep}{0.2cm} % Space between columns \setlength{\headsep}{-12pt} % Reduce space between header and content \setlength{\headheight}{85pt} % If less, LaTeX automatically increases it \renewcommand{\footrulewidth}{0pt} % Remove footer line \renewcommand{\headrulewidth}{0pt} % Remove header line \renewcommand{\seqinsert}{\ifmmode\allowbreak\else\-\fi} % Hyphens in seqsplit % This two commands together give roughly % the right line height in the tables \renewcommand{\arraystretch}{1.3} \onehalfspacing % Commands \newcommand{\SetRowColor}[1]{\noalign{\gdef\RowColorName{#1}}\rowcolor{\RowColorName}} % Shortcut for row colour \newcommand{\mymulticolumn}[3]{\multicolumn{#1}{>{\columncolor{\RowColorName}}#2}{#3}} % For coloured multi-cols \newcolumntype{x}[1]{>{\raggedright}p{#1}} % New column types for ragged-right paragraph columns \newcommand{\tn}{\tabularnewline} % Required as custom column type in use % Font and Colours \definecolor{HeadBackground}{HTML}{333333} \definecolor{FootBackground}{HTML}{666666} \definecolor{TextColor}{HTML}{333333} \definecolor{DarkBackground}{HTML}{8FD435} \definecolor{LightBackground}{HTML}{F8FCF2} \renewcommand{\familydefault}{\sfdefault} \color{TextColor} % Header and Footer \pagestyle{fancy} \fancyhead{} % Set header to blank \fancyfoot{} % Set footer to blank \fancyhead[L]{ \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{C} \SetRowColor{DarkBackground} \vspace{-7pt} {\parbox{\dimexpr\textwidth-2\fboxsep\relax}{\noindent \hspace*{-6pt}\includegraphics[width=5.8cm]{/web/www.cheatography.com/public/images/cheatography_logo.pdf}} } \end{tabulary} \columnbreak \begin{tabulary}{11cm}{L} \vspace{-2pt}\large{\bf{\textcolor{DarkBackground}{\textrm{python regex(regular expression) Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{Nima (nimakarimian)} via \textcolor{DarkBackground}{\uline{cheatography.com/113429/cs/23788/}}} \end{tabulary} \end{multicols}} \fancyfoot[L]{ \footnotesize \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{LL} \SetRowColor{FootBackground} \mymulticolumn{2}{p{5.377cm}}{\bf\textcolor{white}{Cheatographer}} \\ \vspace{-2pt}Nima (nimakarimian) \\ \uline{cheatography.com/nimakarimian} \\ \uline{\seqsplit{www}.nimakarimian.ir} \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Published 21st July, 2020.\\ Updated 21st July, 2020.\\ Page {\thepage} of \pageref{LastPage}. \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Sponsor}} \\ \SetRowColor{white} \vspace{-5pt} %\includegraphics[width=48px,height=48px]{dave.jpeg} Measure your website readability!\\ www.readability-score.com \end{tabulary} \end{multicols}} \begin{document} \raggedright \raggedcolumns % Set font size to small. Switch to any value % from this page to resize cheat sheet text: % www.emerson.emory.edu/services/latex/latex_169.html \footnotesize % Small font. \begin{multicols*}{3} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{re.match()}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{re.match(pattern, "spamspamspam") \newline \#returns True} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{match returns an object representing the match, if not, it returns None.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Sub()}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{re.sub(pattern, repl, string, count=0)}} \newline str = "My name is David. Hi David." \newline pattern = r"David" \newline newstr = re.sub(pattern, "Amy", str) \newline print(newstr) \newline \textgreater{}\textgreater{}\textgreater{} \newline My name is Amy. Hi Amy. \newline \textgreater{}\textgreater{}\textgreater{}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{This method replaces all occurrences of the pattern in string with repl, substituting all occurrences, unless count provided. This method returns the modified string.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{\textasciicircum{}start \&end}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{pattern = r"\textasciicircum{}gr.y\$"} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{The next two metacharacters are \textasciicircum{} and \$. \newline These match the start and end of a string, respectively.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{{[}{]} character classes 3}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{pattern = r"{[}\textasciicircum{}A-Z{]}" \newline \newline if re.search(pattern, "this is all quiet"): \newline print("Match 1") \newline \newline if re.search(pattern, "AbCdEfG123"): \newline print("Match 2") \newline \newline if re.search(pattern, "THISISALLSHOUTING"): \newline print("Match 3") \newline \#\#The pattern {[}\textasciicircum{}A-Z{]} excludes uppercase strings. \newline Note, that the \textasciicircum{} should be inside the brackets to invert the character class. \newline \textgreater{}\textgreater{}\textgreater{} \newline Match 1 \newline Match 2 \newline \textgreater{}\textgreater{}\textgreater{}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{special sequences}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{pattern = r"(.+) \textbackslash{}1" \newline \newline match = re.match(pattern, "word word") \newline if match: \newline print ("Match 1") \newline \newline match = re.match(pattern, "?! ?!") \newline if match: \newline print ("Match 2") \newline \newline match = re.match(pattern, "abc cde") \newline if match: \newline print ("Match 3") \newline \textgreater{}\textgreater{}\textgreater{} \newline Match 1 \newline Match 2 \newline \textgreater{}\textgreater{}\textgreater{} \newline \newline Note, that "(.+) \textbackslash{}1" is not the same as "(.+) (.+)", because \textbackslash{}1 refers to the first group's subexpression, which is the matched expression itself, and not the regex pattern.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{There are various special sequences you can use in regular expressions. They are written as a backslash followed by another character. \newline One useful special sequence is a backslash and a number between 1 and 99, e.g., \textbackslash{}1 or \textbackslash{}17. This matches the expression of the group of that number.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{? metacharacter}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{pattern = r"ice(-)?cream" \newline \newline if re.match(pattern, "ice-cream"): \newline print("Match 1") \newline \newline if re.match(pattern, "icecream"): \newline print("Match 2")} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{The metacharacter ? means "zero or one repetitions".} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{\{\} metacharacters}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{pattern = r"9\{1,3\}\$" \newline \newline if re.match(pattern, "9"): \newline print("Match 1") \newline \newline if re.match(pattern, "999"): \newline print("Match 2") \newline \newline if re.match(pattern, "9999"): \newline print("Match 3") \newline \textgreater{}\textgreater{}\textgreater{} \newline Match 1 \newline Match 2 \newline \textgreater{}\textgreater{}\textgreater{}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{Curly braces can be used to represent the number of repetitions between two numbers. \newline The regex \{x,y\} means "between x and y repetitions of something". \newline Hence \{0,1\} is the same thing as ?. \newline If the first number is missing, it is taken to be zero. If the second number is missing, it is taken to be infinity.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{search() and findall()}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{if re.search(pattern, "eggspamsausagespam"): \newline print("Match") \newline else: \newline print("No match") \newline \newline print(re.findall(pattern, "eggspamsausagespam")) \newline \textgreater{}\textgreater{}\textgreater{} \newline Match \newline {[}'spam', 'spam'{]} \newline \textgreater{}\textgreater{}\textgreater{}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{The function re.search finds a match of a pattern anywhere in the string. \newline The function re.findall returns a list of all substrings that match a pattern.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{. (dot).}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{pattern = r"gr.y" \newline \# this will be grey or gray or anything else except newline} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{This matches any character, other than a new line.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{\textbackslash{}d \textbackslash{}s \textbackslash{}w Special sequences}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{pattern = r"(\textbackslash{}D+\textbackslash{}d)" \newline \newline match = re.match(pattern, "Hi 999!") \newline \newline if match: \newline print("Match 1") \newline \newline match = re.match(pattern, "1, 23, 456!") \newline if match: \newline print("Match 2") \newline \newline match = re.match(pattern, " ! \$?") \newline if match: \newline print("Match 3") \newline \textgreater{}\textgreater{}\textgreater{} \newline Match 1 \newline \textgreater{}\textgreater{}\textgreater{}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{More useful special sequences are \textbackslash{}d, \textbackslash{}s, and \textbackslash{}w. \newline These match digits, whitespace, and word characters respectively. \newline In ASCII mode they are equivalent to {[}0-9{]}, {[} \textbackslash{}t\textbackslash{}n\textbackslash{}r\textbackslash{}f\textbackslash{}v{]}, and {[}a-zA-Z0-9\_{]}. \newline In Unicode mode they match certain other characters, as well. For instance, \textbackslash{}w matches letters with accents. \newline Versions of these special sequences with upper case letters - \textbackslash{}D, \textbackslash{}S, and \textbackslash{}W - mean the opposite to the lower-case versions. For instance, \textbackslash{}D matches anything that isn't a digit.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{{[}{]} character classes 2}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{pattern = r"{[}A-Z{]}{[}A-Z{]}{[}0-9{]}" \newline \newline if re.search(pattern, "LS8"): \newline print("Match 1") \newline \newline if re.search(pattern, "E3"): \newline print("Match 2") \newline \#The pattern in the example above matches strings that contain two alphabetic uppercase letters followed by a digit. \newline \newline \textgreater{}\textgreater{}\textgreater{} \newline Match 1 \newline \textgreater{}\textgreater{}\textgreater{}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{Character classes can also match ranges of characters. \newline The class {[}a-z{]} matches any lowercase alphabetic character. \newline The class {[}G-P{]} matches any uppercase character from G to P. \newline The class {[}0-9{]} matches any digit. \newline Multiple ranges can be included in one class. For example, {[}A-Za-z{]} matches a letter of any cases.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{+ metacharacter}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{pattern = r"g+" \newline \newline if re.match(pattern, "g"): \newline print("Match 1") \newline \newline To summarize: \newline * matches 0 or more occurrences of the preceding expression. \newline + matches 1 or more occurrence of the preceding expression.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{The metacharacter + is very similar to *, except it means "one or more repetitions", as opposed to "zero or more repetitions".} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Groups in metacharacters ()}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{pattern = r"a(bc)(de)(f(g)h)i" \newline \newline match = re.match(pattern, "abcdefghijklmnop") \newline if match: \newline print(match.group()) \newline print(match.group(0)) \newline print(match.group(1)) \newline print(match.group(2)) \newline print(match.groups()) \newline \textgreater{}\textgreater{}\textgreater{} \newline abcdefghi \newline abcdefghi \newline bc \newline de \newline ('bc', 'de', 'fgh', 'g') \newline \textgreater{}\textgreater{}\textgreater{}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{The content of groups in a match can be accessed using the group function. \newline A call of group(0) or group() returns the whole match. \newline A call of group(n), where n is greater than 0, returns the nth group from the left. \newline The method groups() returns all groups up from 1.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{\textbackslash{}A \textbackslash{}Z \textbackslash{}b special sequences}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{pattern = r"\textbackslash{}b(cat)\textbackslash{}b" \newline \newline match = re.search(pattern, "The cat sat!") \newline if match: \newline print ("Match 1") \newline \newline match = re.search(pattern, "We s\textgreater{}cat\textless{}tered?") \newline if match: \newline print ("Match 2") \newline \newline match = re.search(pattern, "We scattered.") \newline if match: \newline print ("Match 3") \newline \textgreater{}\textgreater{}\textgreater{} \newline Match 1 \newline Match 2 \newline \textgreater{}\textgreater{}\textgreater{} \newline \newline "\textbackslash{}b(cat)\textbackslash{}b" basically matches the word "cat" surrounded by word boundaries.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{Additional special sequences are \textbackslash{}A, \textbackslash{}Z, and \textbackslash{}b. \newline The sequences \textbackslash{}A and \textbackslash{}Z match the beginning and end of a string, respectively. \newline The sequence \textbackslash{}b matches the empty string between \textbackslash{}w and \textbackslash{}W characters, or \textbackslash{}w characters and the beginning or end of the string. Informally, it represents the boundary between words. \newline The sequence \textbackslash{}B matches the empty string anywhere else.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{| "or" metacharacter}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{pattern = r"gr(a|e)y" \newline \newline match = re.match(pattern, "gray") \newline if match: \newline print ("Match 1") \newline \newline match = re.match(pattern, "grey") \newline if match: \newline print ("Match 2") \newline \newline match = re.match(pattern, "griy") \newline if match: \newline print ("Match 3") \newline \textgreater{}\textgreater{}\textgreater{} \newline Match 1 \newline Match 2 \newline \textgreater{}\textgreater{}\textgreater{}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{Another important metacharacter is |. \newline This means "or", so red|blue matches either "red" or "blue".} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{named groups \& noncapturing groups}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{pattern = r"(?P\textless{}first\textgreater{}abc)(?:def)(ghi)" \newline \newline match = re.match(pattern, "abcdefghi") \newline if match: \newline \seqsplit{print(match.group("first"))} \newline print(match.groups()) \newline \textgreater{}\textgreater{}\textgreater{} \newline abc \newline ('abc', 'ghi') \newline \textgreater{}\textgreater{}\textgreater{}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{Named groups have the format (?P\textless{}name\textgreater{}...), where name is the name of the group, and ... is the content. They behave exactly the same as normal groups, except they can be accessed by group(name) in addition to its number. \newline Non-capturing groups have the format (?:...). They are not accessible by the group method, so they can be added to an existing regular expression without breaking the numbering.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{* metacharacter}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{pattern = r"egg(spam)*" \newline \newline if re.match(pattern, "egg"): \newline print("Match 1") \newline \newline if re.match(pattern, "eggspamspamegg"): \newline print("Match 2") \newline \newline if re.match(pattern, "spam"): \newline print("Match 3") \newline \textgreater{}\textgreater{}\textgreater{} \newline match 1 \newline match 2 \newline \textgreater{}\textgreater{}\textgreater{} \newline The example above matches strings that start with "egg" and follow with zero or more "spam"s. \newline ................................................} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{The metacharacter * means "zero or more repetitions of the previous thing".} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{{[}{]} character classes}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{pattern = r"{[}aeiou{]}" \newline \newline if re.search(pattern, "grey"): \newline print("Match 1") \newline \newline if re.search(pattern, "qwertyuiop"): \newline print("Match 2") \newline \newline if re.search(pattern, "rhythm myths"): \newline print("Match 3") \newline \newline \#The pattern {[}aeiou{]} in the search function matches all strings that contain any one of the characters defined \newline \textgreater{}\textgreater{}\textgreater{} \newline Match 1 \newline Match 2 \newline \textgreater{}\textgreater{}\textgreater{}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{Character classes provide a way to match only one of a specific set of characters.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Search-\textgreater{}\textgreater{}Group, Start,End,Span}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{match = re.search(pattern, "eggspamsausage") \newline if match: \newline print(match.group()) \newline print(match.start()) \newline print(match.end()) \newline print(match.span()) \newline \textgreater{}\textgreater{}\textgreater{} \newline pam \newline 4 \newline 7 \newline (4, 7) \newline \textgreater{}\textgreater{}\textgreater{}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{The regex search returns an object with several methods that give details about it. \newline These methods include group which returns the string matched, start and end which return the start and ending positions of the first match, and span which returns the start and end positions of the first match as a tuple.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}