正则表达式帮助

发布于 2024-11-09 16:24:57 字数 336 浏览 0 评论 0原文

我正在尝试使用 XSLT 将 XML 转换为平面文本。其中一个节点具有以下内容,我需要提取文本“fail”之前出现的字符串 我计划使用分析字符串函数,但在为其创建正则表达式时遇到了麻烦。我尝试看看是否可以使用前瞻模式,但无法这样做。

有人可以告诉在文本“失败”之前提取方括号内文本的正则表达式是什么:

文本

[[2500, 4500]] fail(expected [[2100, 3000, 4000, 5000, 5400, 6000, 8000, 10000]])

最终输出

[2500, 4500]

I am trying to use XSLT to transform a XML in to flat text. One of the node has the following content and I need to extract the string that appears before the text "fail"
I was planning to use the analyze-string function and having trouble creating the regular expression for it. I tried to see if I could use the lookahead pattern but unable to do so.

Can someone tell what would be the regular expression for extracting the text within square brackets before the text "fail":

Text

[[2500, 4500]] fail(expected [[2100, 3000, 4000, 5000, 5400, 6000, 8000, 10000]])

Final output

[2500, 4500]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浅唱々樱花落 2024-11-16 16:24:58

即使在没有正则表达式的 XSLT 1.0(作为单个 XPath 1.0 表达式)中也可以执行此操作

substring(substring(normalize-space(substring-before(.,' fail')), 2),
          1,
          string-length(substring(
                           normalize-space(substring-before(.,' fail')), 
                           2)
                        ) -1
           )

因为您的目标是在 XSLT 中执行此操作,所以您可以为该复杂表达式的组件定义变量并获取简单易懂的代码:

 <xsl:template match="/">
     <xsl:variable name="vStart" select=
       "normalize-space(substring-before(.,' fail'))"/>

    <xsl:variable name="vnormString" select=
     "normalize-space($vStart)"/>

    <xsl:variable name="vLen" select="string-length($vnormString)"/>

    "<xsl:value-of select="substring($vnormString, 2, $vLen -2)"/>"
 </xsl:template>
</xsl:stylesheet>

当此转换应用于以下 XML 文档时:

<t>[[2500, 4500]] fail(expected [[2100, 3000, 4000, 5000, 5400, 6000, 8000, 10000]]) </t>

生成所需的正确结果:

"[2500, 4500]"

说明:使用标准 XPath 1.0 函数: substring()substring-before()normalize-space()string-length()

You can do this even in XSLT 1.0 (as a single XPath 1.0 expression) without regular expressions:

substring(substring(normalize-space(substring-before(.,' fail')), 2),
          1,
          string-length(substring(
                           normalize-space(substring-before(.,' fail')), 
                           2)
                        ) -1
           )

Because your goal is to do this in XSLT, you can define variables for the components of this complex expression and get a simple and easily understandable code:

 <xsl:template match="/">
     <xsl:variable name="vStart" select=
       "normalize-space(substring-before(.,' fail'))"/>

    <xsl:variable name="vnormString" select=
     "normalize-space($vStart)"/>

    <xsl:variable name="vLen" select="string-length($vnormString)"/>

    "<xsl:value-of select="substring($vnormString, 2, $vLen -2)"/>"
 </xsl:template>
</xsl:stylesheet>

When this transformation is applied on the following XML document:

<t>[[2500, 4500]] fail(expected [[2100, 3000, 4000, 5000, 5400, 6000, 8000, 10000]]) </t>

the wanted, correct result is produced:

"[2500, 4500]"

Explanation: Use of the standard XPath 1.0 functions: substring(), substring-before(), normalize-space() and string-length()

尝蛊 2024-11-16 16:24:57

/\[(\[.+?\])\]失败/并获得$1

/\[(\[.+?\])\] fail/ and get $1.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文