如何查找字符串中的所有数字 [英] How to find all numbers in a string

查看:58
本文介绍了如何查找字符串中的所有数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Ich versuche mit einer Funktion sämtliche Zahlen aus einem 元素 oder String zu ermitteln.Dabei soll die Anzahl der Zahlen 和 ihre Stelligkeit egal sein.Folgende Funktion habe ich bislang geschrieben:

Ich versuche mit einer Funktion sämtliche Zahlen aus einem Element oder String zu ermitteln. Dabei soll die Anzahl der Zahlen und ihre Stelligkeit egal sein. Folgende Funktion habe ich bislang geschrieben:

<xsl:function name="itp:find_num">
<xsl:param name="tmp"/>
<xsl:if test="matches($tmp,'\d+')">
    <xsl:analyze-string select="$tmp" regex="{'\d+'}">
        <xsl:matching-substring>
             <xsl:sequence select="."/>
        </xsl:matching-substring>
        <xsl:non-matching-substring>
            <xsl:value-of select="''"/>
        </xsl:non-matching-substring>
    </xsl:analyze-string>
</xsl:if>
</xsl:function>

Beispiel XML:

Beispiel XML:

<address>street 12, 12345 town<address>

Bei dem Funktionsaufruf soll dann die entsprechende Zahl ausgewählt werden können:

Bei dem Funktionsaufruf soll dann die entsprechende Zahl ausgewählt werden können:

...select="itp:find_num(address)[2]"/>

Zum Beispiel die 2 für die Postleitzahl.Das Problem ist nun, dass in der Sequence auch leere Werte stehen, so dass ich in der Praxis die Postleitzahl nur mit [4] erreiche.

Zum Beispiel die 2 für die Postleitzahl. Das Problem ist nun, dass in der Sequence auch leere Werte stehen, so dass ich in der Praxis die Postleitzahl nur mit [4] erreiche.

Gibt es eine graceere Möglichkeit meine 问题 zu ​​lösen?Und wenn nicht, wie lösche ich die leeren Elemente aus der Sequence??

Gibt es eine elegantere Möglichkeit meine Problem zu lösen? Und wenn nicht, wie lösche ich die leeren Elemente aus der Sequence??

现在是英文:-)

我正在尝试查找元素 oder 字符串中的所有数字.有多少可用数字或它们在字符串中的位置无关紧要.

I'm trying to find all numbers in an element oder string. It shouldn't matter how many numbers are available or at which position they are in the string.

这是我的功能:

<xsl:function name="itp:find_num">
<xsl:param name="tmp"/>
<xsl:if test="matches($tmp,'\d+')">
    <xsl:analyze-string select="$tmp" regex="{'\d+'}">
        <xsl:matching-substring>
        <xsl:if test=". != ''">
            <xsl:sequence select="."/>
        </xsl:if>
        </xsl:matching-substring>
        <xsl:non-matching-substring>
            <xsl:value-of select="''"/>
        </xsl:non-matching-substring>
    </xsl:analyze-string>
</xsl:if>
</xsl:function>

示例 XML

<address>street 12, 12345 town<address>

当我调用我想选择的函数时,我想选择哪个号码:

When I call the function I want to choose, which number I want to pick:

...select="itp:find_num(address)[2]"/>

邮政编码的标准示例 [2].我的问题是,序列中有空元素,所以我必须选择[4]来获取邮政编码.

Par example [2] for the postal code. The Problem I have is, that there are empty elements in the sequence, so that I have to choose [4] to get the postal code.

有没有更简单的方法来解决我的问题?或者有没有办法删除该序列中的所有空元素??

Is there a easier way to solve my problem? Or is there a way to remove all empty elements in that sequence??

谢谢:-)

推荐答案

我会把函数写成

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:mf="http://example.org/mf"
  exclude-result-prefixes="xs mf">

<xsl:function name="mf:find_num" as="xs:integer*">
  <xsl:param name="input" as="xs:string"/>
  <xsl:analyze-string select="$input" regex="[0-9]+">
    <xsl:matching-substring>
      <xsl:sequence select="xs:integer(.)"/>
    </xsl:matching-substring>
  </xsl:analyze-string>
</xsl:function>

<xsl:template match="address">
  <xsl:value-of select="mf:find_num(.)" separator=", "/>
</xsl:template>

</xsl:stylesheet>

当然转换为 xs:integer 是可选的,如果您希望函数返回包含数字的字符串序列,您只需将其更改为 do

Of course converting to xs:integer is optional, if you want the function to return a sequence of strings containing digits you would simply change it to do

<xsl:function name="mf:find_num" as="xs:string*">
  <xsl:param name="input" as="xs:string"/>
  <xsl:analyze-string select="$input" regex="[0-9]+">
    <xsl:matching-substring>
      <xsl:sequence select="."/>
    </xsl:matching-substring>
  </xsl:analyze-string>
</xsl:function>

这篇关于如何查找字符串中的所有数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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