正则表达式帮助
我正在尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
即使在没有正则表达式的 XSLT 1.0(作为单个 XPath 1.0 表达式)中也可以执行此操作:
因为您的目标是在 XSLT 中执行此操作,所以您可以为该复杂表达式的组件定义变量并获取简单易懂的代码:
当此转换应用于以下 XML 文档时:
生成所需的正确结果:
说明:使用标准 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:
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:
When this transformation is applied on the following XML document:
the wanted, correct result is produced:
Explanation: Use of the standard XPath 1.0 functions:
substring()
,substring-before()
,normalize-space()
andstring-length()
/\[(\[.+?\])\]失败/
并获得$1
。/\[(\[.+?\])\] fail/
and get$1
.