分割字符串并获取最后一个元素 [英] Split string and get last element

查看:839
本文介绍了分割字符串并获取最后一个元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一列的值如下:

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

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