gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 15:50:33 24 4
gpt4 key购买 nike

我有一个大字符串,其中包含多个 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com