分割字符串并获取最后一个元素 [英] Split string and get last element
问题描述
假设我有一列的值如下:
Let's say I have a column which has values like:
foo/bar
chunky/bacon/flavor
/baz/quz/qux/bax
即可变数量的字符串,用/
分隔.
I.e. a variable number of strings separated by /
.
在另一列中,我想从这些字符串中的每个字符串中获取最后一个元素,然后将它们在/
上拆分.因此,该列将具有:
In another column I want to get the last element from each of these strings, after they have been split on /
. So, that column would have:
bar
flavor
bax
我不知道这一点.我可以拆分/
并获取一个数组,并且可以看到函数INDEX从该数组中获取特定编号的索引元素,但是找不到在该函数中说最后一个元素"的方法.>
I can't figure this out. I can split on /
and get an array, and I can see the function INDEX to get a specific numbered indexed element from the array, but can't find a way to say "the last element" in this function.
推荐答案
这个比较简单:
=REGEXEXTRACT(A1,"[^/]+$")
您可以使用以下公式:
=REGEXEXTRACT(A1,"(?:.*/)(.*)$")
也可以将其用作ArrayFormula:
And also possible to use it as ArrayFormula:
=ARRAYFORMULA(REGEXEXTRACT(A1:A3,"(?:.*/)(.*)$"))
这里有更多信息:
- the RegExExtract function
- Some good examples of syntax
- my personal list of Regex Tricks
此公式将执行以下操作:
This formula will do the same:
=INDEX(SPLIT(A1,"/"),LEN(A1)-len(SUBSTITUTE(A1,"/","")))
但是A1需要三遍,这是不可能的.
But it takes A1 three times, which is not prefferable.
这篇关于分割字符串并获取最后一个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!