Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If the question really is "What is wrong with this expression?", here's the answer:</p> <ul> <li>You're trying to match newlines with a <code>.*?</code>. You need <code>(?s)</code> for that to work.</li> <li>You have explicit spaces and newlines in the middle of the regex that don't have any corresponding characters in the source text. You need <code>(?x)</code> for that to work.</li> </ul> <p>That may not be <em>all</em> that's wrong with the expression. But just adding <code>(?sx)</code>, turning it into a raw string (because I don't trust myself to mix Python quoting and regex quoting properly), and removing the <code>\n</code> gives me this:</p> <pre><code>r'(?sx)(\\problem{(.*?)}? \\subproblem{(.*?)} (.*?)) (\\problem|\\subproblem|\\end)' </code></pre> <p>That returns 2 matches instead of 0, and it's probably the smallest change to your regex that works.</p> <p>However, if the question is "How can I parse this?", rather than "What's wrong with my existing attempt?", I think impl's solution makes more sense (and I also agree with the point about using regex to parse TeX being usually a bad idea)—-or, even better, doing it in two steps as Regexident does.</p> <hr> <blockquote> <p>if using regex to parse TeX is not good idea, then what method would you suggest to parse TeX?</p> </blockquote> <p>First of all, as a general rule of thumb, if I can't write the regex to solve a problem by myself, I don't want to solve it with a regex, because I'll have a hard time figuring it out a few months from now. Sometimes I break it down into subexpressions, or use <code>(?x)</code> and load it up with comments, but usually I look for another way.</p> <p>More importantly, if you have a real parser that can consume your language and give you a tree (or whatever's appropriate) that you can walk and search—as with, e.g. <code>etree</code> for XML—then you've got 90% of a solution for every problem you're going to come up with in dealing with that language. A quick&amp;dirty regex (especially one you can't write on your own) only gets you 10% of the way to solving the next problem. And more often than not, if I've got a problem today, I'm going to have more of them in the next few months.</p> <p>So, what's a good parser for TeX in Python? Honestly, I don't know. I know <code>scipy</code>/<code>matplotlib</code> has something that does it, so I'd probably look there first. Beyond that, check Google, PyPI, and maybe tex.stackexchange.com. The first things that turn up in a search are <a href="http://www.profv.de/texcaller/" rel="nofollow"><code>Texcaller</code></a> and <a href="http://plastex.sourceforge.net/plastex/" rel="nofollow"><code>plasTeX</code></a>. I have no idea how good they are, or if they're appropriate for your use case, but it shouldn't take long to skim the tutorials and find out.</p> <p>If it turns out that there's nothing out there, and it comes down to writing something myself with, e.g., <a href="http://pyparsing.wikispaces.com" rel="nofollow"><code>pyparsing</code></a> vs. regexes, then it's a tougher choice. Some languages, it's very easy to define just the subset you care about and leave the rest as giant uninterpreted tokens, in which case a real parser will be just as easy as a regex, so you might as well go that way. Other languages, you have to handle half the syntax before you can do anything useful, so I wouldn't even try. I'd have to put a bit of time into thinking about it and experimenting both ways before deciding which way to go.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload