【问题标题】:How to use the correct regex如何使用正确的正则表达式
【发布时间】:2017-12-22 15:43:47
【问题描述】:

我需要获取每个“/”的值,但由于文件中包含特殊字符,我无法获得正确的输出。如何在检查中使用正确的正则表达式?我正在使用 <xsl:analyze-string> 元素来获取值。这是我的示例文件:

输入文件:

<Communication>
   <DialNumber>Phone/+31-3424-27385/null/Phone/+06-32-7890-565/Mobile(Office)/null/+313-(424)-28500/Fax</DialNumber>
</Communication>

预期输出

<Communication>
   <ChannelCode>Phone</ChannelCode>
   <UseCode>null</UseCode>
   <DialNumber>+31-3424-27385</DialNumber>
</Communication>
<Communication>
   <ChannelCode>Phone</ChannelCode>
   <UseCode>Mobile(Office)</UseCode>
   <DialNumber>+06-32-7890-565</DialNumber>
</Communication>
<Communication>
   <ChannelCode>null</ChannelCode>
   <UseCode>Fax</UseCode>
   <DialNumber>+313-(424)-28500</DialNumber>
</Communication>

XSLTCode

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    <xsl:template match="node()|@*">
    <xsl:copy>
        <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
   </xsl:template>
    <xsl:template match="DialNumber">
    <xsl:analyze-string select="normalize-space()" regex="(\w+)/(\w+)/(\w+)">
        <xsl:matching-substring>
            <Communication>
                    <ChannelCode>
                        <xsl:value-of select="regex-group(1)"/>
                    </ChannelCode>
                    <UseCode>
                        <xsl:value-of select="regex-group(3)"/>
                    </UseCode>
                <DialNumber>
                    <xsl:value-of select="regex-group(2)"/>
                </DialNumber>
            </Communication>
        </xsl:matching-substring>
    </xsl:analyze-string>
</xsl:template>
</xsl:stylesheet>

我需要检查 '/' 之后的第一个 3 个单词和接下来的 3 个单词,以及最后 3 个单词。它看起来像这样:

Phone/+31-3424-27385/null

Phone/+06-32-7890-565/Mobile(Office)

null/+313-(424)-28500/Fax

我需要在&lt;Channel&gt; 中分配第一个正则表达式,在&lt;DialNumber&gt; 中分配第二个,在&lt;UseCode&gt; 中分配第三个。

提前感谢您的反馈。

【问题讨论】:

    标签: xml xslt xslt-2.0


    【解决方案1】:

    这里不需要analyze-stringtokenize() 可以正常工作

    <xsl:variable name="tokens" select="tokenize(., '/')"/>
    <xsl:for-each-group select="$tokens" group-adjacent="(position()-1) idiv 3">
            <Communication>
                <ChannelCode>
                    <xsl:value-of select="current-group()[1]" />
                </ChannelCode>
                <UseCode>
                    <xsl:value-of select="current-group()[3]" />
                </UseCode>
                <DialNumber>
                    <xsl:value-of select="current-group()[2]" />
                </DialNumber>
            </Communication>
    </xsl:for-each-group> 
    

    【讨论】:

    • 重要的区别是......?
    • 对不起,我错过了你的答案。不知道怎么弄。
    【解决方案2】:

    如果 - 看起来 - 您的输入是以三人一组的形式组织的,您可以这样做:

    <xsl:template match="Communication">
        <xsl:for-each-group select="tokenize(DialNumber, '/')" group-by="(position()-1) idiv 3">
            <Communication>
                <ChannelCode>
                    <xsl:value-of select="current-group()[1]" />
                </ChannelCode>
                <UseCode>
                    <xsl:value-of select="current-group()[3]" />
                </UseCode>
                <DialNumber>
                    <xsl:value-of select="current-group()[2]" />
                </DialNumber>
            </Communication>        
        </xsl:for-each-group>
    </xsl:template>
    

    演示:http://xsltransform.net/3MvmrA5/1

    【讨论】:

      【解决方案3】:

      如果分隔符是 / 字符,您可以将 analyze-string 替换为以下内容:

      <xsl:analyze-string select="normalize-space()" regex="(.+?)/(.+?)/(.+?)(/|$)" >
      

      在这里,(.+?)/ 执行惰性搜索,匹配/ 之前的一组字符。而/|$ 将考虑斜线后的最后一个标记,因为$ 表示字符串的结尾。

      【讨论】:

      • 感谢您的反馈。
      猜你喜欢
      • 2020-10-29
      • 1970-01-01
      • 2018-12-03
      • 2017-11-23
      • 1970-01-01
      • 2010-10-03
      • 2020-12-14
      • 2018-03-20
      • 2021-08-05
      相关资源
      最近更新 更多