Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use regexmatch with Switch case

I have a list of text on col A and I like to run a switch based on if there is a regexmatch in that cell..

A1 = "https://www.amazon.es/dp/B07PHPXHQS/ref=gw_es_desk_h1_aucc_cr_vd20?pf_rd_p=b1bd6d90-90a7-419a-8a6e-8c943ef52b62&pf_rd_r=XKJEDGQX6TSJ91JF0B6Y"

=switch(A1,REGEXMATCH(A1, "amazon."),"amazon",REGEXMATCH(A1, "lifehacker."),"LFH",REGEXMATCH(A1, "engadget."),"ENG","other")

If the link is amazon.com/whatever it should return "amazon" and so on

I get no error.. but I always get "other" as if regexmatch would not work here.

Any other way to do this?

like image 647
Uniextra Avatar asked Nov 30 '25 16:11

Uniextra


2 Answers

Try this:

=ifs(
REGEXMATCH(A1,"this"),"that",
REGEXMATCH(A1,"this2"),"that2",
REGEXMATCH(A1,"this3"),"this3",
TRUE,elseGoesHere) 

That last line is the ticket don't forget its a new case,value pair so don't forget the preceding coma

like image 152
Jason Taylor Avatar answered Dec 05 '25 12:12

Jason Taylor


It's kind of a category error because in your switch statement you are trying to compare the whole string in A1 to the result of the REGEXMATCH function calls which would actually be TRUE, FALSE and FALSE. To make this work using a switch statement you would need to do something like this:

=iferror(switch(regexextract(A1,"amazon.|lifehacker.|engadget."),"amazon.","amazon","lifehacker.","LFH","engadget.","ENG"),"other")

but I suspect that there are more elegant ways of doing this.

like image 21
Tom Sharpe Avatar answered Dec 05 '25 11:12

Tom Sharpe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!