java - 使用 java 从字符串中查找 mathml

标签 java regex mathml

我有一个大字符串,其中包含多个 mathml。想把一个字符串数组中的全部取出来。使用正则表达式来查找它们。但正则表达式中缺少某些内容,因此它不会提供任何输出。

MathMls 的正则表达式是什么?

示例字符串

Find sum of «math xmlns=\"http://www.w3.org/1998/Math/MathML\"»«mroot»«mrow»«mi»#«/mi»«mi»a«/mi»«/mrow»«mn»3«/mn»«/mroot»«mo»=«/mo»«mroot»«mrow»«mi»#«/mi»«mi»b«/mi»«/mrow»«mn»3«/mn»«/mroot»«/math» and «math xmlns=\"http://www.w3.org/1998/Math/MathML\"»«mo»=«/mo»«msup»«mfenced»«mrow»«mi»#«/mi»«mi»b«/mi»«/mrow»«/mfenced»«mfrac»«mn»1«/mn»«mn»3«/mn»«/mfrac»«/msup»«/math»

从中得到 2 个 mathml

最佳答案

您无法使用 Java 的正则表达式引擎执行此操作,因为这是有效的输入:

<math>
  <apply>
    <plus/>
    <apply>
      <times/>
      <ci>a</ci>
      <apply>
        <power/>
        <ci>x</ci>
        <cn>2</cn>
      </apply>
    </apply>
    <apply>
      <times/>
      <ci>b</ci>
      <ci>x</ci>
    </apply>
    <ci>c</ci>
  </apply>
</math>

即:可以有任意嵌套标签,并且 Java 的正则表达式引擎无法匹配递归模式。您将不得不诉诸some parser处理 MathML 输入。

编辑

Can i consider the entire thing as a string and find for a pattern which matches ? That is what i am trying. And there is not going to be any recursive tags inside another tag. they will be in same level.

在这种情况下,请尝试以下模式:

<math[>\s](?s).*?</math>

或作为字符串文字:

"<math[>\\s](?s).*?</math>"

这意味着:

<math[>\s]   # match `<math` followed by a space or `>`
(?s).*?      # reluctantly match zero or more chars (`(?s)` causes `\r` 
             # and `\n` also to be matched)
</math>      # match `</math>`

关于java - 使用 java 从字符串中查找 mathml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6137438/

相关文章:

regex - sed - 替换几个连续的匹配模式的行

python - 用于删除文本中除允许字符之外的所有字符的正则表达式

regex - 从 R 中的字母数字字符中删除前导零

java - 通过检查点找到穿过迷宫的最小路径

java - Spark 历史日志手动解压

JavaFX如何将 "crop"图形转为按钮

javascript - Javascript 的 MathML 生成算法。任何推荐引用

java - 在 C++ 中从 JNI 调用 Java Jar 代码

html - 用于创建数学方程式的编辑器

java - maven-jar-plugin 找不到我的主类