0

I have the following html string that I need to parse for all latex expressions using regular expression.

A latex expression is anything that is delimited by $$ or \$. For example, $$x^2-x+1 = 0$$ and \$x=34\$ are latex expressions in html string below..

<p>$$x^2-x+1 = 0$$</p>
<p>Another example of inline Latex is \$x=34\$.$$x^2-5x+3 = 0$$</p>
What are the roots of following equation: \$x^2 - 2x + 1 = 0\$?<br class="t-last-br" />

My aim is to get all latex expressions in above string. For this I have used these regular expressions. (I have used the online regex tester at [Regex Storm].1

  • ((\\$)[\W\w]+(\\$)){1} to get latex expressions delimited by \$
  • ((\$\$)[\W\w]+(\$\$)){1} to get latex expressions delimited by $$

Even though I am getting a match with above regular expressions as in screen shots below, but its not matching multiple regular expressions.

Question

What is missing in my above regular expressions that is causing it to not match the multiple latex expressions that are present in the given html string?

First Regular expression matches First regular expression matches

Second Regular expression matches Second regular expression matches

Sunil
  • 20,653
  • 28
  • 112
  • 197
  • 1
    `@"(?s)\\\$.*?\\\$"` – Wiktor Stribiżew Oct 22 '17 at 14:09
  • @WiktorStribiżew, The * in above expression you gave matches 1 or more occurrences, OR 0 or more occurrences? – Sunil Oct 22 '17 at 14:13
  • 1
    @WiktorStribiżew, I tried with `((\$\$)[\W\w]+?(\$\$)){1}` and `((\\\$)[\W\w]+?(\\\$)){1}`. Both worked. Also using * in place + in these expressions worked. Thanks. – Sunil Oct 22 '17 at 14:15
  • 1
    @Sunil the following regex will capture both possibilities and not require a single-line match `(\\?\$\$.*?\\?\$\$)|(\\?\$.*?\\?\$)` – Jeremy Farrell Oct 22 '17 at 14:19
  • @JeremyFarrell, Yes your expression gives all latex expressions which is better than using separate regular expressions for inline and paragraph latex expressions. Do you want to post this as an answer? Thanks. – Sunil Oct 22 '17 at 14:22
  • @Sunil I would love too, but this question is marked as a duplicate so I am not able to. – Jeremy Farrell Oct 22 '17 at 14:25
  • @JeremyFarrell, I didn't realize that an answer cannot be posted for duplicate questions.. Thanks anyways. – Sunil Oct 22 '17 at 14:27

0 Answers0