如何使用正确的正则表达式 [英] How to use the correct regex

查看:36
本文介绍了如何使用正确的正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要获取每个/"的值,但由于文件中包含特殊字符,我无法获得正确的输出.如何使用正确的正则表达式进行检查?我正在使用 元素来获取值.这是我的示例文件:

I need to get the value for each '/' but I can't get the correct output because of the special characters included in the file. How can I use the correct regex to be used in the checking? I am using the <xsl:analyze-string> element to get the value. Here is my sample file:

输入文件:

<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>

XSLT代码

<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 个单词.它看起来像这样:

I need to check the 1st 3 words after '/' and the next 3 words again, and the last 3 words. It looks like this:

电话/+31-3424-27385/null

电话/+06-32-7890-565/手机(办公室)

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

我需要在 中分配的第一个正则表达式, 中的第二个和 中的第三个.

The 1st regex I need to assign in <Channel>, 2nd in <DialNumber> and 3rd in <UseCode>.

提前感谢您的反馈.

推荐答案

如果 - 看起来 - 您的输入以三个为一组进行组织,您可以简单地做:

If - as it seems - your input is organized in groups of three, you could do simply:

<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

这篇关于如何使用正确的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆