$re = '/(\$)([^$]+)(\$)/m';
$str = '\\documentclass{article}
%\\usepackage[T1]{fontenc}
%\\usepackage{textcomp}
%\\usepackage[math]{iwona}
\\usepackage[lf,minionint]{MinionPro}
\\usepackage{a4,bm}
%\\usepackage{dotlessj}
\\pagestyle{empty}
\\begin{document}
\\begin{center}
{\\Large{\\bfseries Test of the font {\\scshape MinionPro}.}}\\bigskip\\\\
{\\Large\\verb|\\usepackage[lf,minionint]{MinionPro}|}
%\\medskip\\\\
%{\\verb|\\usepackage[romanfamily=bright-osf]{lucimatx}|} if you want old-style figures (like this: \\oldstylenums{1695})\\\\
\\end{center}
The default math mode font is $Math\\ Italic$. This should not be
confused with ordinary \\emph{Text Italic} -- notice the different spacing\\,!
\\verb|\\mathbf| produces bold roman letters: $ \\mathbf{abcABC} $.
If you wish to embolden complete formulas,
use the \\verb|\\boldmath| command \\emph{before} going into math mode.
This changes the default math fonts to bold.
\\begin{tabular}{ll}
\\texttt{normal} & $ x = 2\\pi \\Rightarrow x \\simeq 6.28 $\\\\
\\texttt{mathbf} & $\\mathbf{x} = 2\\pi \\Rightarrow \\mathbf{x} \\simeq 6.28 $\\\\
\\texttt{boldmath} & {\\boldmath $x = \\mathbf{2}\\pi \\Rightarrow x
\\simeq{\\mathbf{6.28}} $}\\\\
\\end{tabular}
\\smallskip
Greek is available in upper and lower case:
$\\alpha,\\beta \\dots \\Omega$, and there are special
symbols such as $ \\hbar$.
%The following letters should be upright: $\\upGamma, \\upDelta\\dots \\upOmega$.
Digits in formulas $1, 2, 3\\dots$ may differ from those in text: 1, 2, 3\\dots
There is a calligraphic alphabet \\verb|\\mathcal| for upper case letters
$ \\mathcal{ABCDE}\\dots $.
%and there are letters for number sets: $\\mathbb{A\\dots Z} $, which are produced using \\verb|\\mathbb|.
\\noindent
This font has both lining figures (13589, default) and oldstyle figures (\\oldstylenums{13589}, select with {\\tt$\\backslash$oldstylenums\\{..\\}}).
%{\\em \\swshape{I}t \\swshape{A}lso \\swshape{H}as \\swshape{S}wash \\swshape{I}talics} {\\tt$\\backslash$swshape\\{..\\}}
\\\\
{\\fontseries{c}\\selectfont there is also a condensed weight} {\\tt$\\backslash$fontseries\\{c\\}$\\backslash$selectfont}
\\begin{equation}
\\phi(t)=\\frac{1}{\\sqrt{2\\pi}}
\\int^t_0 e^{-x^2/2} dx
\\end{equation}
\\begin{equation}
\\prod_{j\\geq 0}
\\left(\\sum_{k\\geq 0}a_{jk} z^k\\right)
= \\sum_{k\\geq 0} z^n
\\left( \\sum_{{k_0,k_1,\\ldots\\geq 0}
\\atop{k_0+k_1+\\ldots=n} }
a{_0k_0}a_{1k_1}\\ldots \\right)
\\end{equation}
\\begin{equation}
\\pi(n) = \\sum_{m=2}^{n}
\\left\\lfloor \\left(\\sum_{k=1}^{m-1}
\\lfloor(m/k)/\\lceil m/k\\rceil
\\rfloor \\right)^{-1}
\\right\\rfloor
\\end{equation}
\\begin{equation}
\\{\\underbrace{%
\\overbrace{\\mathstrut a,\\ldots,a}^{k\\ a\'s},
\\overbrace{\\mathstrut b,\\ldots,b}^{l\\ b\'s}}
_{k+1\\ \\mathrm{elements}} \\}
\\end{equation}
\\begin{displaymath}
\\mbox{W}^+\\
\\begin{array}{l}
\\nearrow\\raise5pt\\hbox{$\\mu^+ + \\nu_{\\mu}$}\\\\
\\rightarrow \\pi^+ +\\pi^0 \\\\[5pt]
\\rightarrow \\kappa^+ +\\pi^0 \\\\
\\searrow\\lower5pt\\hbox{$\\mathrm{e}^+
+\\nu_{\\scriptstyle\\mathrm{e}}$}
\\end{array}
\\end{displaymath}
\\begin{displaymath}
\\frac{\\pm
\\left|\\begin{array}{ccc}
x_1-x_2 & y_1-y_2 & z_1-z_2 \\\\
l_1 & m_1 & n_1 \\\\
l_2 & m_2 & n_2
\\end{array}\\right|}{
\\sqrt{\\left|\\begin{array}{cc}l_1&m_1\\\\
l_2&m_2\\end{array}\\right|^2
+ \\left|\\begin{array}{cc}m_1&n_1\\\\
n_1&l_1\\end{array}\\right|^2
+ \\left|\\begin{array}{cc}m_2&n_2\\\\
n_2&l_2\\end{array}\\right|^2}}
\\end{displaymath}
text accents: \\`{a},\\\'{a},\\"{a},\\^{a}
may differ from math accents:
\\begin{displaymath}
\\mbox{ acute=}\\acute{a}
\\mbox{ grave=}\\grave{a}
\\mbox{ ddot=}\\ddot {a}
\\mbox{ tilde=}\\tilde{a}
\\mbox{ bar=}\\bar {a}
\\mbox{ breve=}\\breve{a}
\\mbox{ check=}\\check{a}
\\mbox{ hat=}\\hat {a}
\\mbox{ vec=}\\vec {a}
\\mbox{ dot=}\\dot {a}
\\end{displaymath}
dotlessi=\\i\\
dotlessj=\\j\\
dagger=$\\dagger$\\ \\ \\
\\verb|\\bm{x}|\\ $\\bm{x}$
%$\\hbar$ $\\hslash$
\\end{document}
';
preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);
// Print the entire match result
var_dump($matches);
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for PHP, please visit: http://php.net/manual/en/ref.pcre.php