In case you missed the announcement: The Alteryx One Fall Release is here! Learn more about the new features and capabilities here
ACT NOW: The Alteryx team will be retiring support for Community account recovery and Community email-change requests after December 31, 2025. Set up your security questions now so you can recover your account anytime, just log out and back in to get started. Learn more here
Start Free Trial

Alteryx Designer Desktop Discussions

Find answers, ask questions, and share expertise about Alteryx Designer Desktop and Intelligence Suite.
SOLVED

REGEX conditional formula

gwiz
8 - Asteroid

In a field, I'd like to extract text before first underscore if just one underscore. and if there are two underscores, extract text between the two. I can't seem to get a regex formula to work.

 

Example:

aywkhqeq_abcde_weyfb

abc_weyw

 

Is there a regex that can match abcde in the first one and abc in the second?

3 REPLIES 3
fmvizcaino
17 - Castor
17 - Castor

Hi @gwiz ,

 

Yes, there is a formula.

Use the parse method and the following expression: .*?([^_]+)_[^_]+$

 

Let me know if that works for you.

Best,

Fernando Vizcaino

MarqueeCrew
20 - Arcturus
20 - Arcturus

@gwiz ,

 

I simplified the conditions and constructed this formula:

 

IF REGEX_COUNTMATCHES([Field1],"_") = 1 THEN
	REGEX_Replace([Field1], "(.*?)_.*", '$1')
ELSE
	REGEX_Replace([Field1], ".*?_(.*)_.*", '$1')
ENDIF

 

Cheers,


Mark 

Alteryx ACE & Top Community Contributor

Chaos reigns within. Repent, reflect and restart. Order shall return.
Please Subscribe to my youTube channel.
gwiz
8 - Asteroid

@MarqueeCrew 

@fmvizcaino 

 

Both those approaches worked. Appreciate the responses!

 

Cheers

Labels
Top Solution Authors