java - 如何使用正则表达式查找字符串中的Latex表达式

标签 java regex latex

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

其他人告诉我一个Python模式:

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

预期:

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

但是我在android中使用它,Java不支持expresspattern。谁可以为我写一个JavaPattern或者给我一些提示?

最佳答案

在java中,只有很少的修改。

1. Escape the backslash surrounding $

2. Make use of double quotation mark

您的新代码现在看起来像这样

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

注意:确保导入模式类

关于java - 如何使用正则表达式查找字符串中的Latex表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32737389/

相关文章:

regex - 匹配 URL 的路径,减去文件扩展名

php - 多语言站点的 .htaccess 规则

documentation - 使用 ocamldoc 包含数学方程

r - Sweave/knitr 实现中的 mean 和 sem 函数

latex - 如何在 Latex 中定位图形?

java - 如何让 JButton 在再次单击时重复其操作?

java - Oracle插入分区绑定(bind)JDBC中的分区名称

java - 我的 Volley 应用程序崩溃了,可能是在 listView 代码上

java - 应用程序可在Nougat +版本上运行,但在棉花糖和 Lollipop 上崩溃

regex - 使用正则表达式进行简单的 Groovy 替换