By lots of internet searches, and dumb luck, I wrote a REGEX_REPLACE formula that does exactly what I want. I'm hoping someone can explain why it works.
I wanted to evaluate a string containing 0-x underscores. If there were less than 2, then return everything to the left of the first underscore. If there were more than 2, then return everything to the left of the second to last underscore.
So, if the string was "abc_def", then I wanted to return "abc".
If it was "abc_def_hij_klm", then I only wanted "abc_def".
I did it using the following formula:
if REGEX_CountMatches([text_in], "_") < 2 then
REGEX_Replace([text_in], "(.+)_(.+)","$1") else
REGEX_Replace([text_in], "(.+)_(.+_.+)","$1") endif
I understand the if REGEX_CountMatches([text_in], "_") < 2 part. What I don't understand is why the last two lines work like they do. Specifically, why/how does the pattern work?
I have attached the workflow.