返回字符串中第 5 个和第 6 个空格之间的字符串

Returning the string between the 5th and 6th Spaces in a String

我有一列字符串如下所示:

Target Host: dcmxxxxxxc032.erc.nam.fm.com Target Name: dxxxxxxgsc047.erc.nam.fm.com Filesystem /u01 has 4.98% available space - fallen below warning (20) or critical (5) threshold.

列名称是[描述]

我想要返回的子字符串是 (dxxxxxxgsc047.erc.nam.fm.com)

这个数据唯一一致的是,想要的字符串出现在字符串中第5次和第6次出现空格“”之间,以及短语"Target Name: "之后子串的长度各不相同,但总是以另一个“”结尾,因此我试图抓住第 5 个和第 6 个空格之间的子字符串。

我试过了

MID([Description],((FIND([Description],"Target Name: "))+13),FIND([Description]," ",((FIND([Description],"Target Name"))+14)))

但这不起作用。

(编辑:我们使用 Tableau 8.2,Tableau 9 的唯一功能不能成为解决方案的一部分,谢谢!)

预先感谢您的帮助。

我不了解 Tableau,但也许是这样的?

MID(
    MID([Description], FIND([Description],"Target Name: ") + 13, 50),
    1,
    FIND(MID([Description], FIND([Description],"Target Name: ") + 13, 50), " ")
)

在 Tableau 9 中,您可以在公式中使用正则表达式,这使任务变得更简单:

REGEXP_EXTRACT([Description], "Target Name: (.*?) ")

或者,在 Tableau 9 中,您可以使用新的 FINDNTH 函数:

MID(
     [Description],
     FINDNTH([Description]," ", 5) + 1, 
     FINDNTH([Description]," ", 6) - FINDNTH([Description]," ", 5) - 1
   )

在 Tableau 9 之前,您必须使用类似于您尝试过的字符串操作方法,只需要非常小心地进行算术运算并提供正确的参数(MID 中的第三个参数是length,不是结束字符的index,所以需要减去起始字符的index):

MID(
   [Description]
   , FIND([Description], "Target Name:") + 13
   , FIND([Description], " ", FIND([Description], "Target Name:") + 15)
     - (FIND([Description], "Target Name:") + 13)
)

好吧,您需要找到 "Target name: " 然后是它后面的“ ”,并不难。为了更清楚,我将分成 3 个字段(您可以将所有内容混合在一个字段中)。顺便说一句,你的方向是正确的,但 MID() 的最后一个字段应该是字符串长度,而不是字符位置

[开始]:

FIND([Description],"Target name: ")+13

[结束]:

FIND([Description]," ",[start])

最后是您需要的:

MID([Description],[start]+1,[end]-[start]-1)

这应该可以。如果你想追求第 5 和第 6 个 " " 方法,我建议你找到每个 " " 直到第 6 个。

[第 1 名]:

FIND([Description], " ")

[第二名]:

FIND([Description], " ",[1st] + 1)

等等。那么:

MID([Description],[5th]+1,[6th]-[5th]-1)

一个简单的解决方案-

SPLIT( [Description], " ", 3 )

此 returns 描述字符串的一个子字符串,使用 space 定界符将字符串分成一系列标记。

The string is interpreted as an alternating sequence of delimiters and tokens. So for the string abc-defgh-i-jkl, where the delimiter character is ‘-‘, the tokens are abc, defgh, i and jlk. Think of these as tokens 1 through 4. SPLIT returns the token corresponding to the token number. When the token number is positive, tokens are counted starting from the left end of the string; when the token number is negative, tokens are counted starting from the right. - Tableau String Functions