\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{frozencookie} \pdfinfo{ /Title (regular-expressions.pdf) /Creator (Cheatography) /Author (frozencookie) /Subject (Regular Expressions 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}{00A326} \definecolor{LightBackground}{HTML}{EFF9F1} \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{Regular Expressions Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{frozencookie} via \textcolor{DarkBackground}{\uline{cheatography.com/197909/cs/41837/}}} \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}frozencookie \\ \uline{cheatography.com/frozencookie} \\ \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Published 23rd December, 2023.\\ Updated 23rd December, 2023.\\ 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}{p{0.64701 cm} x{4.32999 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{普通字符}} \tn % Row 0 \SetRowColor{LightBackground} . & 匹配除换行符(\textbackslash{}n、\textbackslash{}r)之外的任何单个字符 \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} {[}ABC{]} & 匹配 {[}...{]} 中的所有字符 \tn % Row Count 3 (+ 1) % Row 2 \SetRowColor{LightBackground} {[}\textasciicircum{}ABC{]} & 匹配除了 {[}...{]} 中字符的所有字符 \tn % Row Count 5 (+ 2) % Row 3 \SetRowColor{white} {[}A-Z{]} & {[}A-Z{]} \seqsplit{表示一个区间,匹配所有大写字母} \tn % Row Count 7 (+ 2) % Row 4 \SetRowColor{LightBackground} {[}a-z{]} & {[}a-z{]} \seqsplit{表示一个区间,匹配所有小写字母} \tn % Row Count 9 (+ 2) % Row 5 \SetRowColor{white} {[}\textbackslash{}s\textbackslash{}S{]} & 匹配所有 \tn % Row Count 11 (+ 2) % Row 6 \SetRowColor{LightBackground} (a|b) & a或b \tn % Row Count 12 (+ 1) % Row 7 \SetRowColor{white} (...) & 组 \tn % Row Count 13 (+ 1) % Row 8 \SetRowColor{LightBackground} \{n\} & \{\}前的内容重复n次 \tn % Row Count 14 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{p{0.4977 cm} x{4.4793 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{字符}} \tn % Row 0 \SetRowColor{LightBackground} \textbackslash{}c x & 匹配由 x 指明的控制字符 \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \textbackslash{}s & \seqsplit{匹配任何空白字符,包括空格、制表符、换页符等等} \tn % Row Count 3 (+ 2) % Row 2 \SetRowColor{LightBackground} \textbackslash{}S & \seqsplit{匹配任何非空白字符} \tn % Row Count 4 (+ 1) % Row 3 \SetRowColor{white} \textbackslash{}d & 匹配一个数字字符 \tn % Row Count 5 (+ 1) % Row 4 \SetRowColor{LightBackground} \textbackslash{}D & \seqsplit{匹配一个非数字字符} \tn % Row Count 6 (+ 1) % Row 5 \SetRowColor{white} \textbackslash{}w & \seqsplit{匹配字母、数字、下划线} \tn % Row Count 7 (+ 1) % Row 6 \SetRowColor{LightBackground} \textbackslash{}W & \seqsplit{匹配非字母、数字、下划线} \tn % Row Count 8 (+ 1) % Row 7 \SetRowColor{white} \textbackslash{}xn & 匹配 n,其中 n \seqsplit{为十六进制转义值。十六进制转义值必须为确定的两个数字长} \tn % Row Count 11 (+ 3) % Row 8 \SetRowColor{LightBackground} \textbackslash{}On & 匹配 n,其中 n 为八进制转义值 \tn % Row Count 13 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{p{0.4977 cm} x{4.4793 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{量词}} \tn % Row 0 \SetRowColor{LightBackground} * & \seqsplit{匹配前面的子表达式零次或多次} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} + & \seqsplit{匹配前面的子表达式一次或多次} \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} ? & \seqsplit{匹配前面的子表达式零次或一次} \tn % Row Count 6 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{\seqsplit{给一个量词加一个?使它变成非贪婪模式}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{p{0.4977 cm} x{4.4793 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{锚点}} \tn % Row 0 \SetRowColor{LightBackground} \textasciicircum{} & \seqsplit{字符串的开头,或多行模式中行的开头} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \textbackslash{}A & 字符串的开头 \tn % Row Count 3 (+ 1) % Row 2 \SetRowColor{LightBackground} \$ & \seqsplit{字符串的结束,或多行模式中行的结束} \tn % Row Count 5 (+ 2) % Row 3 \SetRowColor{white} \textbackslash{}Z & 字符串的结束 \tn % Row Count 6 (+ 1) % Row 4 \SetRowColor{LightBackground} \textbackslash{}b & \seqsplit{匹配一个单词边界,也就是指单词和空格间的位置} \tn % Row Count 8 (+ 2) % Row 5 \SetRowColor{white} \textbackslash{}B & 匹配非单词边界 \tn % Row Count 9 (+ 1) % Row 6 \SetRowColor{LightBackground} \textbackslash{}\textless{} & 单词开头 \tn % Row Count 10 (+ 1) % Row 7 \SetRowColor{white} \textbackslash{}\textgreater{} & 单词结尾 \tn % Row Count 11 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{p{0.4977 cm} x{4.4793 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{转义序列}} \tn % Row 0 \SetRowColor{LightBackground} \textbackslash{} & 转义跟随的字符 \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \textbackslash{}Q & 开始文本序列 \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} \textbackslash{}E & 结束文本序列 \tn % Row Count 3 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{\seqsplit{转义是一种将在正则表达式中具有特殊含义的字符按原始含义处理,而不是将其作为特殊字符处理的方法。}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{p{0.59724 cm} x{4.37976 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{特殊字符}} \tn % Row 0 \SetRowColor{LightBackground} \textbackslash{}n & 换行符 \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \textbackslash{}r & 回车 \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} \textbackslash{}t & 制表符 \tn % Row Count 3 (+ 1) % Row 3 \SetRowColor{white} \textbackslash{}v & 竖向制表符 \tn % Row Count 4 (+ 1) % Row 4 \SetRowColor{LightBackground} \textbackslash{}f & 换页符 \tn % Row Count 5 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{p{0.4977 cm} x{4.4793 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{模式}} \tn % Row 0 \SetRowColor{LightBackground} g & 全局匹配 \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} i & \seqsplit{将匹配设置为不区分大小写} \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} m & \seqsplit{多行匹配,使边界字符} \textasciicircum{} 和 \$ \seqsplit{匹配每一行的开头和结尾,记住是多行,而不是整个字符串的开头和结尾} \tn % Row Count 6 (+ 4) % Row 3 \SetRowColor{white} s & 默认情况下的圆点 . 是匹配除换行符 \textbackslash{}n \seqsplit{之外的任何字符,加上} s 修饰符之后, . 中包含换行符 \tn % Row Count 10 (+ 4) % Row 4 \SetRowColor{LightBackground} x & \seqsplit{模式中允许注释和空格} \tn % Row Count 11 (+ 1) % Row 5 \SetRowColor{white} e & 评估更换 \tn % Row Count 12 (+ 1) % Row 6 \SetRowColor{LightBackground} U & 非贪婪模式 \tn % Row Count 13 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{0.9954 cm} x{3.9816 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{断言}} \tn % Row 0 \SetRowColor{LightBackground} \seqsplit{?=pattern} & \seqsplit{正向肯定预查,在任何匹配pattern的字符串开始处匹配查找字符串} \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} ?!pattern & \seqsplit{正向否定预查,在任何不匹配pattern的字符串开始处匹配查找字符串} \tn % Row Count 6 (+ 3) % Row 2 \SetRowColor{LightBackground} ?\textless{}=pattern & \seqsplit{反向肯定预查,与正向肯定预查类似,只是方向相反} \tn % Row Count 9 (+ 3) % Row 3 \SetRowColor{white} ?\textless{}!pattern & \seqsplit{反向否定预查,与正向否定预查类似,只是方向相反} \tn % Row Count 12 (+ 3) % Row 4 \SetRowColor{LightBackground} ?\textgreater{} & 一次性次表达式 \tn % Row Count 13 (+ 1) % Row 5 \SetRowColor{white} () & 捕获组,使用括号 () \seqsplit{将一部分正则表达式模式组合成一个捕获组} \tn % Row Count 16 (+ 3) % Row 6 \SetRowColor{LightBackground} ?() & 条件 {[}if then{]} \tn % Row Count 17 (+ 1) % Row 7 \SetRowColor{white} ?()| & 条件 {[}if then else{]} \tn % Row Count 18 (+ 1) % Row 8 \SetRowColor{LightBackground} ?\# & 注释 \tn % Row Count 19 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{p{0.74655 cm} x{4.23045 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{字符串替换}} \tn % Row 0 \SetRowColor{LightBackground} \$n & \seqsplit{替换已编号的组,替换字符串中n捕获组所匹配的最后一个子字符串,其中n是捕获组的索引} \tn % Row Count 4 (+ 4) % Row 1 \SetRowColor{white} \$\{name\} & 替换命名组 \tn % Row Count 6 (+ 2) % Row 2 \SetRowColor{LightBackground} \$\$ & \seqsplit{将在替换的字符串中插入文本"\$"字符} \tn % Row Count 8 (+ 2) % Row 3 \SetRowColor{white} \$\& & 替换整个匹配项 \tn % Row Count 9 (+ 1) % Row 4 \SetRowColor{LightBackground} \$` & \seqsplit{替换匹配项前的文本} \tn % Row Count 10 (+ 1) % Row 5 \SetRowColor{white} \$' & \seqsplit{替换匹配项后的文本} \tn % Row Count 11 (+ 1) % Row 6 \SetRowColor{LightBackground} \$+ & 替换最后捕获的组 \tn % Row Count 12 (+ 1) % Row 7 \SetRowColor{white} \$\_ & \seqsplit{替换整个输入字符串} \tn % Row Count 13 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}