简体   繁体   中英

How to use regular expressions to find the Latex expression in a string

private static final String TEXT = "this a test 1: $\\$$,test 2: $\\underline{\\rm defund}$, test 3:$$\\underline{\\rm defund}$$";

Others told me a python pattern:

re.compile(r'(?=([^\\]?))(?:(?P<bound2>\$\$)|\$)(.*?[^\\])(?:(?(bound2)\$\$|\$))', re.S)

Expected:

$\$$ or \$, $\underline{\rm defund}$ or \underline{\rm defund}

But I use it in android,Java doesn't support express pattern .Who can write a Java Pattern for me or give me some tips?

In java, there are just little modifications.

1. Escape the backslash surrounding $

2. Make use of double quotation mark

Your new code would now look like this

Pattern.compile("(?=([^\\]?))(?:(?P<bound2>\\$\\$)|\\$)(.*?[^\\])(?:(?(bound2)\\$\\$|\\$))");

Note: Make sure you import your pattern class

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM