# Regular expression to select equation selects multiple consecutive ones

I am trying to select all equations, i.e. text between dollar signs using Find-Replace with the regular expression \$.+\$. This should be straightforward, but the problem of too much being selected appears. Some equations are correctly selected, for example $\theta\in\{H,L\}$ in the MWE below, but sometimes consecutive equations and the text between them are selected, for example $k\in\mathbb{R}$ bla bla bla iff $k\geq v_{H}$ in the MWE. Both in comments and uncommented code.

How to exclude the text between equations from selection?

MWE:

\documentclass{article}

\begin{document}

$\theta\in\{H,L\}$, but
% $k\in\mathbb{R}$ bla bla bla iff $k\geq v_{H}$.
$\Pr(H)=\mu$.
% $1$.
Then $\theta$ bla bla bla $c_{\theta}$ bla bla bla $v_{\theta}$. Assume
$c_{L},v_{L}<c_{H}<\bar{v}:=\mu v_{H} + (1-\mu) v_{L}$.\footnote{
bla bla bla $c_{H}<\bar{v}$ bla bla bla if $c_{H}<\bar{v}$.
}

\end{document}


Edit: \$.+?\$ selects nothing in my example.

Other regex questions on tex.stackexchange do not address this question. The fact that sometimes the regex selects correctly, sometimes not suggests that the problem is not with the regex syntax in general, but with TeXstudio.

• + is a greedy quantifier, i.e., it matches as much as it can. You want the lazy quantifier +?: \$.+?\$. However if some equation happens to have a \$, or even a commented $ it will fail. Either way, I think your question is off-topic here. Regex questions are probably better at stackoverflow.com Sep 3, 2019 at 23:50
• @PhelypeOleinik The lazy quantifier is too lazy - \$.+?\$ selects nothing in my example. Sep 3, 2019 at 23:52
• I don't know about TeXStudio, but in the editor I'm using right now it works (as it should): i.stack.imgur.com/ZSWrn.png. Here it also works: regex101.com/r/oI3SYe/1 Sep 3, 2019 at 23:55

For regex-based search and replace, TeXstudio uses Qt's QRegExp class internally. This regex implementation provides lazy quantifiers, but only as a global option for a whole pattern. The documentation says:

QRegExp's quantifiers are the same as Perl's greedy quantifiers [...]. Non-greedy matching cannot be applied to individual quantifiers, but can be applied to all the quantifiers in the pattern. For example, to match the Perl regexp ro+?m requires:

QRegExp rx("ro+m");
rx.setMinimal(true);


[...] If minimal is false, matching is greedy (maximal) which is the default.

TeXstudio doesn't set this flag when performing the search, so it seems the current version just doesn't support searching with lazy quantifiers.

As a workaround you can make your patterns more specific, e.g. instead of searching for \$.+\$ you could search for \$[^$]+\$ which will disallow extra $s within the outer $s of a match. • This is, of course, the correct answer (@SanderHeinsalu). Sep 4, 2019 at 19:52 Too-long-for-a-comment proof that \$.+?\$ works (and I, procrastinating). l3regex also matches the math environments (of course, since were in TeX, commented lines are still commented, so they aren't matched): \documentclass[12pt]{article} \usepackage{xparse} \usepackage{xcolor} \usepackage{soul} \ExplSyntaxOn \regex_new:N \l__matchregex_regex \tl_new:N \l__matchregex_tl \NewDocumentEnvironment { matchregex } { m m b } { \regex_set:Nn \l__matchregex_regex {#1} \cs_set:Npn \__matchregex_function:n ##1 {#2} \tl_set:Nn \l__matchregex_tl {#3} \regex_replace_all:NnN \l__matchregex_regex { \c{__matchregex_function:n} \cB{ \0 \cE} } \l__matchregex_tl \tl_use:N \l__matchregex_tl } { } \ExplSyntaxOff \begin{document} \begin{minipage}{10cm} \begin{matchregex}{ \$.+?\$}{\hl{#1}}$\theta\in\{H,L\}$, but %$k\in\mathbb{R}$bla bla bla iff$k\geq v_{H}$.$\Pr(H)=\mu$. %$1$. Then$\theta$bla bla bla$c_{\theta}$bla bla bla$v_{\theta}$. Assume$c_{L},v_{L}<c_{H}<\bar{v}:=\mu v_{H} + (1-\mu) v_{L}$.\footnote{ bla bla bla$c_{H}<\bar{v}$bla bla bla if$c_{H}<\bar{v}$. } \end{matchregex} \end{minipage} \end{document}  • \$.+?\\$ may work in some versions of TeXstudio, though not in mine (2.12.6 Using Qt Version 5.9.1, compiled with Qt 5.9.1 R). The workaround of using a different editor or xparse solves the problem, so I will accept the answer. Sep 4, 2019 at 1:42
• @SanderHeinsalu As I said, in a sensible regex implementation (with PCRE or something similar) that should work. If it doesn't, I think you should report to the maintainers (if your version is up-to-date). My answer doesn't actually answer your question, but as I said in the comment your question is sort of off-topic here, so I don't think you'll get anything else. I'd probably report :-) Sep 4, 2019 at 1:52