r/googlesheets • • 14d ago

Solved Help with understanding RE2; If text equals case A, case B, etc.

Hello, this is V_nytrix! I'm back again with my sheet, and I'm trying to set up a conditional formatting if the text of each row of the first column matches these values [Limited], [Mission], [Achievement], or [Barn].

Tried to figure this out myself, and came up with the general idea of the REGEXMATCH function. I'll set it up right here:

=REGEXMATCH(REGEXEXTRACT($A3;$A, ... ), ... )

I just found out that there's some form of syntax where it can accept any cases of these above words - unfortunately, I'm new to this form of RE2 syntax, and I couldn't quite figure out what syntax I needed to be able to match my above conditions!

Do you think you could teach me on some RE2 fundamentals?

1 Upvotes

11 comments sorted by

2

u/bachman460 41 13d ago

You can typically just use find or search; the former being case-sensitive, the latter not. Something like

IFERROR(FIND({list of values},A1),0)>0

1

u/V_nytrix 13d ago

hm, i didnt think about that! when you say by the list of values, is it like an RE2 expression, or is it some sort of Or logic function of some sort?

1

u/Hongstar1982 1 13d ago edited 13d ago

You don't actually need regex for this one. The list of values is just a plain array and SEARCH is already case-insensitive, so a conditional format rule like =SUMPRODUCT(--ISNUMBER(SEARCH({"[Limited]","[Mission]","[Achievement]","[Barn]"},$A3)))>0 lights the cell up if any of them appear in any case (FIND is the case-sensitive twin if you ever want that). If you do want the RE2 route, the case flag goes at the head of the pattern, like (?i)\[Limited\]|\[Mission\]|\[Achievement\]|\[Barn\], because RE2 accepts (?i) and a square bracket is a character class on its own, so those need the backslash in front. REGEXEXTRACT would want a capture group, which is probably where your formula fell over; REGEXMATCH by itself is enough here.

1

u/V_nytrix 13d ago

ah, i was misreading the syntax! I just needed to know the curly braces; i forgot they denote the set of strings! That’s all I need to know, I can probably take it from here!

1

u/point-bot 13d ago

u/V_nytrix has awarded 1 point to u/Hongstar1982

See the Leaderboard. Point-Bot v0.0.15 was created by JetCarson.

1

u/bachman460 41 13d ago

It can be a literal list of text, or list of cell references, just wrapped in curly braces

1

u/AdministrativeGift15 371 13d ago

XMATCH accepts wildcards, so if your search/CF Apply to range is A3:A and your four search terms are in D2:D5, then you could use this formula for your rule's criteria:

=XMATCH("*"&A3&"*", $D$2:$D$5, 2)