Alteryx Designer Desktop Discussions

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

How to remove part of a string that comes after last special character

Patricia2
7 - Meteor

Hi,

 

I'm looking to remove everything that comes after a special character in a string. The thing is that is not a specific number of letters after that character (it can vary) and also the special character appears many times before. For example from a string like this: "coffee_milk_sugar" I'd only want to remove "_sugar" and keep the rest. Is there a tool that I could use to only remove whatever comes after the LAST underscore??

 

Thank you!!

7 REPLIES 7
AbhilashR
15 - Aurora
15 - Aurora

Regex tool would be able to help you achieve this ask, or you could write a formula in the formula tool if that helps.

Attached is a sample implementation of the Regex tool.

Patricia2
7 - Meteor

Thank you, but according to that RegEx it would find any mention of the underscore and remove that. I what to be sure it only removes what comes after the last underscore in the string. Is that possible? Thanks

AbhilashR
15 - Aurora
15 - Aurora

It depends on how you setup your RegEx string, The sample solution I shared will always look for the last underscore and give you the string after it. Below is a sample screenshot.

AbhilashR_0-1585669450165.png

 

Patricia2
7 - Meteor

Ok i see, i think it might work then. However what I need is also to get it removed from the original string. Would a REGEX_Replace with a similar command work to do that?

 

Thanks

AbhilashR
15 - Aurora
15 - Aurora

I have reattached an updated solution that modifies the existing field using regex_replace using both the Regex, and Formula tool.

fmvizcaino
17 - Castor
17 - Castor

Hi @Patricia2 ,

 

Only another possibility, you could use the following function in your formula tool.

REGEX_Replace([Field1], '(.*)_([^_]+)', '$1')

 

Best,

Fernando Vizcaino

Patricia2
7 - Meteor

It worked, thank you

Labels