Google电子表格:拆分字符串并获取最后一个元素 [英] Google spreadsheet: split string and get last element
问题描述
假设我有一个列,它的值如下:
foo / bar
chunky / bacon / flavor
/ baz / quz / qux / bax
也就是说, /\".
在另一列中,我想从每个字符串中获取最后一个元素,并将其分割为/。所以,那一栏应该有
bar
flavor
bax
我无法弄清楚这一点。我可以分割/并得到一个数组,我可以看到函数index从数组中获取一个特定的编号索引元素,但是找不到在这个函数中说最后一个元素的方法。
这个是简单的:
= REGEXEXTRACT(A1,[^ /] + $)
你可以使用这个公式:
= REGEXEXTRACT(A1,(?: 。* /)(。*)$)
也可以将其用作ArrayFormula:
= ARRAYFORMULA(REGEXEXTRACT(A1:A3,(?:。* /)(。*)$))
以下是更多信息:
这个公式也会这样做:
= INDEX(SPLIT(A1,/),LEN(A1)-len(SUBSTITUTE(A1,/,)))
但是它需要A1三次,这是不可预测的。
Let's say I have a column which has values like
foo/bar
chunky/bacon/flavor
/baz/quz/qux/bax
Ie, 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
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.
Edit: this one is simplier:
=REGEXEXTRACT(A1,"[^/]+$")
You could use this formula:
=REGEXEXTRACT(A1,"(?:.*/)(.*)$")
And also possible to use it as ArrayFormula:
=ARRAYFORMULA(REGEXEXTRACT(A1:A3,"(?:.*/)(.*)$"))
Here's some more info:
This formula will do the same:
=INDEX(SPLIT(A1,"/"),LEN(A1)-len(SUBSTITUTE(A1,"/","")))
But it takes A1 three times, which is not prefferable.
这篇关于Google电子表格:拆分字符串并获取最后一个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!