问题描述
我想知道是否可以使用 analyze-string
并在 RegEx 中设置多个组,然后将所有匹配的组存储在变量中以备后用.
I was wondering if it is possible to use analyze-string
and set multiple groups within the RegEx and then store all of the matching groups in variables to use later on.
像这样:
<xsl:analyze-string regex="^Blah\s+(\d+)\s+Bloo\s+(\d+)\s+Blee" select=".">
<xsl:matching-substring>
<xsl:variable name="varX">
<xsl:value-of select="regex-group(1)"/>
</xsl:variable>
<xsl:variable name="varY">
<xsl:value-of select="regex-group(2)"/>
</xsl:variable>
</xsl:matching-substring>
</xsl:analyze-string>
这实际上不起作用,但这正是我所追求的,我知道我可以将 analyze-string
包装在一个变量中,但这对于我的每个组来说似乎都很愚蠢必须处理正则表达式,效率不高,我应该能够处理一次正则表达式并存储所有组以备后用.
This doesn't actually work, but that's the sort of thing I'm after, I know I can wrap the analyze-string
in a variable, but that seems daft that for every group I have to process the RegEx, not very efficient, I should be able to process the regex once and store all of the groups for use later on.
有什么想法吗?
推荐答案
做得好
<xsl:variable name="groups" as="element(group)*">
<xsl:analyze-string regex="^Blah\s+(\d+)\s+Bloo\s+(\d+)\s+Blee" select=".">
<xsl:matching-substring>
<group>
<x><xsl:value-of select="regex-group(1)"/></x>
<y><xsl:value-of select="regex-group(2)"/></y>
</group>
</xsl:matching-substring>
</xsl:analyze-string>
</xsl:variable>
帮助?这样你就有了一个名为 groups
的变量,它是一个带有捕获的 group
元素的序列.
help? That way you have a variable named groups
which is a sequence of group
elements with the captures.
这篇关于XSL 分析字符串 ->将子串匹配到多个变量中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!