Google-sheets – Extracting specific phrases from a cell and echoing them in another

formulasgoogle sheetsgoogle-sheets-arrayformula

Longtime learner, first time poster here!

I was wondering if anyone knew how to extract specific data points (phrases) from a Google Sheets cell and echo them in another cell.

enter image description here

For instance, in my example I have Vehicle year/make/model/trim etc. All in my B column. I would like to be able to extract for instance the trim into it's own column.
So for instance I would need the formula to search for XL, XLT, and STX and if one of those is found, then echo that exact phrase out in another cell.

I would and can post a few things I have tried, but I honestly haven't gotten very close yet. Mostly ended up echoing out the complete cell value or #NA.

Also please note that the format for the original cell might change so an ideal solution would not rely on the information being sandwiched between two "known variables" but instead simply search for specific variables and echo those when found.

Any help would be much appreciated!

Best Answer

The functions you're looking for are REGEXMATCH combined with OR and IF.
So IF will test three conditions (with OR) and then if OR returns a TRUE boolean, copy the cell's content, else do whatever.

=IF(or(REGEXMATCH(A1,"XL"),REGEXMATCH(A1,"XLT"),REGEXMATCH(A1,"STX")),A1,"no match")

Here's a way to do it with only one formula

ArrayFormula(IFS(ArrayFormula(REGEXMATCH(A1:A5,"XL")),A1:A5,ArrayFormula(REGEXMATCH(A1:A5,"XLT")),A1:A5,ArrayFormula(REGEXMATCH(A1:A5,"STX")),A1:A5))

EDIT :
Here's a way to return the tested key :

       =ArrayFormula(
IF(ArrayFormula(REGEXMATCH(A2:A6,"XL")),"XL ",)
&IF(ArrayFormula(REGEXMATCH(A2:A6,"XLT ")),"XLT",)
&IF(ArrayFormula(REGEXMATCH(A2:A6,"STX")),"STX",)
) 

There's a condition for each "key" then it retuns it in case of a match. I used & to concatenate the result if case of multiples keys inside the data.

Example : "Sample with XL and XLT" will return "XL XLT"

Edit 2 :

Same thing with Regextract to get rid of IF.

=ArrayFormula(ArrayFormula(iferror(REGEXEXTRACT(A2:A6,"XL")))&"-"&ArrayFormula(iferror(REGEXEXTRACT(A2:A6,"XLT")))&"-"&ArrayFormula(iferror(REGEXEXTRACT(A2:A6,"STX"))))