Is there a way to REGEXMATCH from a range of cells from A1:A1000 for example?

人盡茶涼 提交于 2020-07-19 18:04:32

问题


Basically, I am trying to find a way to search through a list of phrases and highlight/extract or identify any phrase or cell that contains a phrase or word from a separate column/list.

To put this into context, I have a list of "search terms" that have triggered my Google ads, this list contains phrases or expressions that people have entered into the google search engine.

I also have a list of "negative keywords" that I have used to block ads from showing when certain words or phrases are entered into Google. For example, if I have the word "nursery" in my negative keyword list, then it should stop an ad from showing if someone enters the phrase "best nursery near me"

I have then placed both these lists within an excel spreadsheet in separate columns, so one column has the search terms, and another column has the negative keywords.

What I want to do is search through all the search terms and highlight a cell it if any word or phrase within that cell matches the phrase from the negative keywords list. In the example above, the cell containing the phrase "best nursery near me" would be highlighted or extracted as the phrase "nursery" is in the negative keyword list.

REGEXMATCH in google sheets does do this, however, I don't know of a way for it to select multiple expressions, it does allow multiple expressions but only by doing say (A1|A2|A3), the issue with this is I have over 1000 cells so would want a way to select all of them at once, like (A1:A1000)

here is a link to the Google Sheet, if possible I'd prefer to be able to do it in Excel. https://docs.google.com/spreadsheets/d/1yLTswjrpwf2owhX4YxPavUY441WlQsnzv3StoP-ilmc/edit?usp=sharing


回答1:


=ARRAYFORMULA(IF(REGEXMATCH(A2:A, TEXTJOIN("|", 1, C:C)), A2:A, ))


=ARRAYFORMULA(IFERROR(REGEXEXTRACT(A2:A, TEXTJOIN("|", 1, C:C))))


=ARRAYFORMULA(TRIM(TRANSPOSE(QUERY(TRANSPOSE(IF(REGEXMATCH(IFERROR(
 SPLIT(A2:A, " ")), "^"&TEXTJOIN("|^", 1, C:C)), A2:A, )),,999^99))))


=ARRAYFORMULA(TRIM(TRANSPOSE(QUERY(TRANSPOSE(IF(REGEXMATCH(IFERROR(
 SPLIT(A2:A, " ")), "^"&TEXTJOIN("$|^", 1, C:C)&"$"), A2:A, )),,999^99))))


=ARRAYFORMULA(IF(IF(TRIM(TRANSPOSE(QUERY(TRANSPOSE(IF(REGEXMATCH(IFERROR(
 SPLIT(A2:A, " ")), "^"&TEXTJOIN("$|^", 1, C:C)&"$"), A2:A, )),,999^99)))<>"", 
 TRIM(TRANSPOSE(QUERY(TRANSPOSE(IF(REGEXMATCH(IFERROR(
 SPLIT(A2:A, " ")), "^"&TEXTJOIN("$|^", 1, C:C)&"$"), A2:A, )),,999^99))),
 TRIM(TRANSPOSE(QUERY(TRANSPOSE(IF(REGEXMATCH(A2:A, 
 "^"&TEXTJOIN("$|^", 1, C:C)&"$"), A2:A, )),,999^99))))<>"",
 IF(TRIM(TRANSPOSE(QUERY(TRANSPOSE(IF(REGEXMATCH(IFERROR(
 SPLIT(A2:A, " ")), "^"&TEXTJOIN("$|^", 1, C:C)&"$"), A2:A, )),,999^99)))<>"", 
 TRIM(TRANSPOSE(QUERY(TRANSPOSE(IF(REGEXMATCH(IFERROR(
 SPLIT(A2:A, " ")), "^"&TEXTJOIN("$|^", 1, C:C)&"$"), A2:A, )),,999^99))),
 TRIM(TRANSPOSE(QUERY(TRANSPOSE(IF(REGEXMATCH(A2:A, 
 "^"&TEXTJOIN("$|^", 1, C:C)&"$"), A2:A, )),,999^99)))), 
 TRIM(TRANSPOSE(QUERY(TRANSPOSE(IF(REGEXMATCH(IF(REGEXMATCH(A2:A, "\s"), A2:A, ), 
 TEXTJOiN("|", 1, C:C)), A2:A, )),,999^99)))))




回答2:


There is a much simpler way to do this. Don't overcomplicate things.

Say you want to search for a match from a list of cities.

  1. Put your list of cities in one tab.
  2. Make them into lowercase for easier lookup since search terms are all in lowercase. You can do this by adding a new column and using the LOWER function.
  3. Go back to your cell that has the list of search phrases.
  4. In any blank cell out of the way (off to the side on the top row is a good place) put this formula: CITY LIST FORMULA: =TEXTJOIN("|",1,'vlookup city'!B$2:B$477) (if your tab is named 'vlookup city' and your cities are in column B of that tab)

  5. Add a new column next to your search terms, or pick an existing one where you want to put your "match found" info.

  6. In that new column, add this formula (if your data starts in row 4 and you put the City List formula in cell G3:) =REGEXMATCH(A4,G$4)

  7. Fill the formula all the way down your list. You can double-click the little blue square in the bottom right corner of the cell, or grab-and-drag all the way to the bottom of the list.

Ba-ding! It will search for any one of those city names, anywhere in your search phrase.

If the search phrase contains at least one matching term, it will return "True."

You can then add extra features on your formula to make it return something else. For example: =IF(REGEXMATCH(A4,G$4), "match found", "no match found")

This is a super lightweight solution that won't slow your sheet down too much and is easy to use.

enter image description here

https://docs.google.com/spreadsheets/d/1XAIDB98r2CGu7hL3ISirErDPNlgT6lVt-TCG0qI1uTE/edit?usp=sharing



来源:https://stackoverflow.com/questions/56577313/is-there-a-way-to-regexmatch-from-a-range-of-cells-from-a1a1000-for-example

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!