views:

176

answers:

1

I need a formula to extract the last two words in a cell using openoffice. For example a cell contains the words: "enjoy the rest of your day" I would like to extract "your day" using a formula. I know how to extract the last word:

=RIGHT(A1;LEN(A1)-FIND("*";SUBSTITUTE(A1;" ";"*";LEN(A1)-LEN(SUBSTITUTE(A1;" ";"")))))

which results in "day". But i need a formula for the last two words.

+1  A: 

SEARCH supports regular expressions, so use

=RIGHT(A1, LEN(A1) - SEARCH("[^ ]+ +[^ ]+$", A1) + 1)

When I use semicolons as below, Calc silently substitutes commas, but the OP reports success entering it this way:

=RIGHT(A1; LEN(A1) - SEARCH("[^ ]+ +[^ ]+$"; A1) + 1)
Greg Bacon
thanks for your answer but it gave me an error: #NAME? (Invalid name), please help
sami
What value does `=SEARCH("[^ ]+ [^ ]+$", "apple orange banana")` give you? With version 3.1.1, I get `7`. Does the value in `A1` have multiple spaces as separators?
Greg Bacon
=SEARCH("[^ ]+ [^ ]+$", "apple orange banana") gives me the same error i had before. I am using openoffice 2.4. And there are no multiple spaces as separators.
sami
could the problem be that i'm using openoffice 2.4?
sami
Probably. I don't have a 2.4 install to use for testing. That's an old version. Any chance of upgrading?
Greg Bacon
i have upgraded to 3.1.1 and it gave me an error when testing =SEARCH("[^ ]+ [^ ]+$", "apple orange banana") -> Err 508 (Error in bracketing)
sami
After a copy-and-paste of the formula from your comment, the result is `7`. I could reproduce the Err 508 only by deleting the left parenthesis or adding an extra right parenthesis on the end, but doing both ways produced messageboxes warning about the bad formula. Could there have been a copy-and-paste problem?In Tools > Options > OpenOffice.org Calc > Calculate, make sure "Enable regular expressions in formulas" is checked.
Greg Bacon
IT WORKED!!! Your formula had to be with semi-colons(;) instead of commas(,) Thanks a lot.
sami
I'm glad it helped!
Greg Bacon