Substitute in Google Sheet but Ignore Particular string - google-sheets

I am using this formula in Google Sheets to substitute some string of words.
=ArrayFormula(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(Z2:Z36000,"# ", "OF ")," ALUMINUM BAG ", ""),"BOX IN",""),"BOX OF",""),"",""))
The data
DATA Column Expected Results
BOX OF BEER BEER
BOX IN BASEMENT BASEMENT
BOX OF GIFT BOX OF GIFT
I would like to exclude "BOX OF GIFT" with Substitute. Can i do that? OR any other solutions?

You could use in B2:
=REGEXREPLACE(A2,"^(?:(BOX OF GIFT$)|BOX (?:OF|IN) )(.*)","$1$2")
Or:
=IF(A2="BOX OF GIFT",A2;REGEXREPLACE(A2,"\bBOX (?:OF|IN)\b *",""))
I'll see if I can come up with a cleaner option...

Related

Search Criteria as a range

I am trying to find a way to use a range as a search criteria.
The problem is as follows:
Below is a range of names in NAMES!A2:A.
Names
Benjamin Douglas
Caitlin Enriquez
Cristina Butler
Emily Furse
Jenny Ford
Liana Fowler
Lowri Donald
Rachel McBride
Rochelle Guy
Samina Swanson
Sol Wills
Sonya Cantu
On another sheet, I allow input of multiple names in another sheet in the range SEARCH!B:B. Upon entering a range of names, I would like it to return one string that contains any string within SEARCH!B2:B into SEARCH!A2
Searching Criteria
Jennifer
Jennie
Jenny
Jenni
Jenn
Jen
And following the search criteria the formula would return in SEARCH!A2 the string Jenny Ford
I have tried multiple formulas including INDEX and FILTER but none of them accept a range as a search criteria.
I have included an example sheet if needed: link
Use this
=INDEX( NAMES!A2:A,MATCH(TRUE, REGEXMATCH(NAMES!A2:A, TEXTJOIN("|",1,C2:C)),0),1)
Help links
INDEX - MATCH - REGEXMATCH - TEXTJOIN
try:
=FILTER(NAMES!A2:A, REGEXMATCH(NAMES!A2:A, "(?i)"&TEXTJOIN("|", 1, C2:C)))

Google Sheets: If one cell contains "string" in a row range, return T/F

I have some cells with multiple lines of text, and I'm looking for a specific word in that text. So for the example below, every row that contains the word "Canada" should return as "TRUE"
A
B
C
Jeffrey
Canada, Male
Hi, I'm from Canada! I love to play hockey
Priyanka
UK, Female
I grew up in London
Victoria
USA, Female
I'm originally from Canada but now live in NYC
I guess I'm thinking about this the wrong way, because IF + SEARCH seems to only return an exact match for the selected cell(s), which of course is an issue for cells with unique text. I'm really not sure what else to try.
In your situation, how about using REGEXMATCH as follows?
Sample formula:
=ARRAYFORMULA(IF(REGEXMATCH(A1:A&B1:B&C1:C,"Canada"),TRUE,""))
In this sample formula, the values of columns "A", "B" and "C" are joined and Canada is searched using REGEXMATCH.
Result:
When this sample formula is used, the following result is obtained.
Reference:
REGEXMATCH
use:
=INDEX(REGEXMATCH(A1:A&B1:B&C1:C; "Canada"))
be aware that regex is case sensitive so if you need so use:
=INDEX(REGEXMATCH(LOWER(A1:A&B1:B&C1:C); "canada"))

How can I search for data in Google Sheets using multiple keywords?

I am trying to make a search box in google sheets which searches data based on keywords. This is specific to a spare parts inventory system. Each item is defined by 5 columns namely Type, Part Name, Part Number, Price, Stock.
One example of an item would be "Lockstitch","Needle Bar Bush","229-10312 / 331-2312", "500", "3000".
In a separate sheet, I am trying to make a search box where I put in keywords like "Lockstich" "Bush" and the result should display.
I have tried concatenating the column name into one string and concatenating the keywords and matching them using QUERY function, but the problem that arises is that when I try to search "lockstitch bush" it doesn't give result because it does not contain in the string "Lockstitch Needle Bar Bush 229-10312 / 331-2312 500 3000"
I want the code to separately search each keyword in the combined string and give out results matching the keywords
you can try like this:
=FILTER(A2:E6, REGEXMATCH(TRANSPOSE(QUERY(TRANSPOSE(LOWER(A2:E6)),,999^99)),
TEXTJOIN("|", 1, SPLIT(LOWER(H1), " "))))
or more strict:
=ARRAYFORMULA(QUERY({A2:E6, TRANSPOSE(QUERY(TRANSPOSE(A2:E6),,999^99))},
"select Col1,Col2,Col3,Col4,Col5
where "&TEXTJOIN(" and ", 1, IF(TRANSPOSE(SPLIT(H1, " "))<>"",
"lower(Col6) contains '"&TRANSPOSE(SPLIT(LOWER(H1), " "))&"'", ))&"", 0))

Google Sheets: "Bob Smith" --> "bsmith" formula?

I'm trying to pull data from another Google Sheet to feed another Google Sheet. I need to pull from a full name field, which will have something like, "Bob Smith" and then I need to have it rewrite into the new Google Sheet as "bsmith".
Basically, "Get first letter of the first string, then concatenate the entire second string, and then make all lowercase."
So far I've gotten =LEFT(A28,1) working to grab the first letter of a string, but then not sure how to grab the second word and then concatenate.
To get the 2nd word you need to FIND() the first space then read from that position + 1 to the end of the string using MID(). & is used for concatenation.
=lower(left(A28,1) & mid(A28, find(" ", A28) + 1, len(A28)))
Try this for a Google sheet specific solution:
=LOWER(REGEXREPLACE(A2,"^(\w).*?(\w+$)","$1$2"))
It uses REGEX, a much more sophisticated engine and easily adaptable to variations than LEFT and/or MID.
Shorter:
=lower(left(A28)&index(split(A28," "),2))
(Assumes only ever two words.)

Categorize cells by keywords

I am not certain if excel can do this but I am trying to simplify the data dump that I get from twitter.
Basically what I would like to do is this:
If the tweet (in Column A) contains apple OR orange OR pear then it can be classified (in Column B) as "fruit" BUT if it has carrot OR squash OR lettuce it will be classified as "vegetable". If it has none of these then can be classified as "none"
Is this possible?
Thanks in advance.
Here is using array constant and range.
=IF(SUMPRODUCT(IF(ISERROR(SEARCH({"apple","orange","pear"},A1)),0,1))>0,"Fruit",IF(SUMPRODUCT(IF(ISERROR(SEARCH({"carrot","squash","lettuce"},A1)),0,1))>0,"Vegetable","None"))
Now for example, both fruit and vegetable are present in a string, it will always test for fruit first since that is the way the formula was arranged. (e.g. "more apple on salad than lettuce" will return "Fruit").
You can also use a range that contains your list instead of the array constant.
For example, you can put your fruit list in Column C (C1:C3) and your vegetable list in Column D (D1:D3). Your formula would then be:
=IF(SUMPRODUCT(IF(ISERROR(SEARCH(C$1:C$3,A1)),0,1))>0,"Fruit",IF(SUMPRODUCT(IF(ISERROR(SEARCH(D$1:D$3,A1)),0,1))>0,"Vegetable","None"))
But you need to enter it as Array Formula using Ctrl+Shift+Enter.
Same results and rule apply when both fruit and vegetable appear on a string. HTH.
Sure.
Try this formula
=IF(
OR(
NOT(ISERROR(SEARCH("apple",A1))),
NOT(ISERROR(SEARCH("pear",A1))),
NOT(ISERROR(SEARCH("orange",A1)))
),
"fruit",
IF(
OR(
NOT(ISERROR(SEARCH("carrot",A1))),
NOT(ISERROR(SEARCH("squash",A1))),
NOT(ISERROR(SEARCH("lettuce",A1)))
),
"veggie",
"none"
)
)

Resources