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
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.
To split a string and get the last element of the array, call the split() method on the string, passing it the separator as a parameter, and then call the pop() method on the array, e.g. str. split(','). pop() . The pop() method will return the last element from the split string array.
To get the last element of the array, we can pass in index -1 as the start index to return an array that has the last element of the array. We don't need the end index since the default value for the end index is the last index of the array. We use [0] to get the first element of the array returned by slice .
Use the str. rsplit() method with maxsplit set to 1 to split a string and get the last element. The rsplit() method splits from the right and will only perform a single split when maxsplit is set to 1 .
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.
Also possible, perhaps best on a copy, with Find:
.+/
(Replace with
blank) and Search using regular expressions
ticked.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With