I want to find char $ using lua features, but only if there is no \ char before char $. For this I create the pattern:


However, Lua ignores the first part of my pattern and finds only %$. Please help me to solve my problem.

Yes, I know that lua has a very bad pattern parser, but I hope I can find $ if it is either the first character of the line or if it does not have \ before. Thanks so very much for the help.

  • Could you please post a small example document that can be used to reproduce the problem? You seem to use an optional quantifier ?, so the group can be skipped by the regex matcher if it is not present. – siracusa Aug 14 at 1:23
  • @siracusa ok,for example in formula \$a-b=0\$ it find two $,but it mustn't do it. ? i used,because $ can be in start of line,so in this case no character before $. – Aleksandr Kozlovskiy Aug 14 at 1:33
  • 2
    I will just reiterate, what people on this site have already told you countless times: It won't work! You can't capture math mode like that. – Henri Menke Aug 14 at 2:05
  • 1
    Please edit your question or title to make it clear if you are asking about Lua patterns or if you have loaded a Lua regex library (the syntax and functionality of Lua patterns and regular expressions are quite different), also make it clearer what you try to match, originally you say "before it no char" which I would take to mean beginning of line, but later you say "char not equals ." – David Carlisle Aug 14 at 6:56
  • 1
    Lua patterns are not regular expressions, so the title (and your comments mentioning regex) are very confusing. – David Carlisle Aug 14 at 10:47

Your question is very unclear and there are no regular expressions involved here (Lua patterns are quite distinct from regex)

But I think you are looking for something like this, which prints a string involving \$ and $ and then prints it with the \$ still intact but all unquoted $ replaced.

z="aaa \\$ $ x=1$"


tmp=string.gsub(z,'\\%$', 'QUOTEDOLLAR')

tmp=string.gsub(tmp,'%$', '!!!')

newz=string.gsub(tmp,'QUOTEDOLLAR', '\\$' )


executing this with texlua produces the terminal output

aaa \$ $ x=1$
aaa \$ !!! x=1!!!
  • No,i want,that for example line 1$ replace on 1( or 1) (depend of setuation),or for example line $a replace on (a or )a depends of setuation. It works for me,but if i have,for example,line \$a must be the same,but with my regex it not works and it replace on \( or \) (depends of situation),so the condition of my regexp ([^\]?) ignored. – Aleksandr Kozlovskiy Aug 14 at 12:31
  • 2
    @AleksandrKozlovskiy you only asked about pattern matching your question makes no mention of what you want to do with the match, I just used replace to !!! as an example, The technique is the same, first replace every \$ with a "safe string" then do whatever you want to do then change the safe string back to \$. (please stop calling these regexp!!) – David Carlisle Aug 14 at 12:33
  • Ok,how i must call this instead of regex. As for your idea,it very difficult to find line,which not exists in documents. For example i can replace each \$ on (,but this part of line can be exists in document,so your method not always give correct results. Ok,advice me please good regexp library for lua,which compatible with lualatex. – Aleksandr Kozlovskiy Aug 14 at 13:35
  • But it exists very small probability,that this line will be in document. Also i replace $ and $$ not on the whole line,but only before meeting %,ie if it exists comments,in which we have $ or $$,it mustn't replace by anymore. So i also check,that before % i haven't \. Yes,i can,probably,for $ and % chack,if before it exists \,using indexes of search,but this do my code more longer,so i want to do it with lua patterns or regexp,but i dont know regex library for lua,which compatible with lualatex. – Aleksandr Kozlovskiy Aug 14 at 14:19
  • Ok,it's a very pitty,that lua patterns ignore my first part of regex. I not wanted use your idea,because in this case my code will be more longer and i hope,that someone advice to me good regex library for lua,but now i will use your idea. About catcodes,maybe it exists way understood in lua,what catcode have $ or another character in this moment to avoid failing of my code. In one document i noticed,that exists chars \\$,ie $ before which we have double \. Can i replace \\$ on $ in this case,or $ with double \ it another command? – Aleksandr Kozlovskiy Aug 15 at 11:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.