Alteryx Designer Desktop Discussions

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

Remove specific trailing characters from a string.

zchotani
7 - Meteor

How can I remove " - 2" from the end of the string only. Example:

 

 

ABS - 2  --->  ABS

AB - 2S ---> AB - 2S    <-- Note this one was not replaced at " - 2" was not at the very end.

 

Thank you,

Zak

8 REPLIES 8
binuacs
20 - Arcturus

@zchotani One way of doing this

 

REGEX_Replace([String], '(.*)(\s\-\s\d+)(.*)', '$1$3')

 

binuacs_0-1675807315152.png

 

ShankerV
17 - Castor

Hi @zchotani 

 

One way of doing this.

 

ShankerV_0-1675807320474.png

 

zchotani
7 - Meteor

Need to better understand how to read regex. As in what in the formula make it such that it only replaces " - 2" at the end of the string and ignores the same character in leading or middle positions. I ended up using TrimRight....

zchotani
7 - Meteor

Thank you @shankerV... TrimRight worked flawlessly!

zchotani
7 - Meteor

Shanker,
I am running into an issue with this formula:


For example:

 

Its converting "Baton Rouge - Bricksome - 2" to   ---->  "Baton Rouge - Bricksome" which is correct

 

but its also incorrectly  converting: "Novi - W 12" to --> "Novi - W 1"


 

 

We only want to trim out " - 2" from the strings if its at the trailing end of the string...

Any suggestions?

binuacs
20 - Arcturus

@zchotani Attaching the updated workflow for your use case. The Regex_Match formula will check whether the string ends with - 2 then it will remove the - 2 else write the same string

binuacs_0-1677181681334.png

 

zchotani
7 - Meteor

@binuacs This worked flawlessly. If its not too much to ask, can you help me understand how the formula works... Specially what part looks for " - 2" at the end and how Match and replace formulas are working together?

binuacs
20 - Arcturus

@zchotani The formula Regex_Match([Field1], '.* \- 2$' - checks whether the given string ends with the string " - 2" , if that is true the regex_replace function split the string into two parts one with string up to the space before the - sign .

 

$-represents the end of the string.

2$ - means all string ends with 2

 

 

REGEX_Replace([Field1], '(.*)\s\- 2', '$1') here 

.*  will read the text up to the last string which is right to it. If you look at the above formula after the .* the next is \s(which is space) then \- (which is hyphen) then <space>2. so the .* will return anything before \s\- 2 , 

 

$1 represents the first group, here (.*) - is the group (Note: group is identified by whatever is in the bracket if you have two items in the bracket it will be $1 and $2)

Labels