Advent of Code is back! Unwrap daily challenges to sharpen your Alteryx skills and earn badges along the way! Learn more now.

Alteryx Designer Desktop Discussions

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

Removing punctuation only at end

JamesBills
8 - Asteroid

Hi, 

Wanted to remove any punctuation or special characters at the start or end of the field. But leave it in the middle. 

Fields vary in length and don't always end on a text character.

 

OLD                                            NEW

LAGOS,\                                     LAGOS

RIO-BRAZIL                               RIO-BRAZIL
,NIGERIA,\LAGOS                     NIGERIA,\LAGOS
PHILLIPS66-5,                           PHILLIPS66-5

 

 

Thanks, 

James

5 REPLIES 5
binuacs
21 - Polaris

@JamesBills  one way of doing this with the replaceChar()

https://help.alteryx.com/20221/designer/string-functions

 

binuacs_1-1660642314946.png

 

binuacs_0-1660642241799.png

 

DataNath
17 - Castor
17 - Castor

How's this @JamesBills? The expression basically will capture anything between the first and last instance of word characters (the \w notation).

 

(\w.+\w)

 

DataNath_0-1660642929163.png

 

EDIT: I always forget that underscores are included in word characters and so if you had a record like '_,NIGERIA,\LAGOS_', this would throw off solutions that rely on the \w or \W notation alone. The following expression should address this as it includes _ in the non-capture along with non-word characters.

([^_\W].+[^_\W])

DataNath_0-1660643609074.png

mceleavey
17 - Castor
17 - Castor

Hi @JamesBills ,

 

To do this you will need to specify that you are removing punctuation from the beginning and end of your string. I have built this in Regex to determine one or more none word character from the beginning of the string, OR one or more non-word character from the end of the string. You can change this if you are expecting numbers to be simply a manual character set if you like.

 

The regex looks like this:

 

^\W+|\W+$

 

Configure your regex tool to use the Replace function, and leave the Replacement Text section blank. This will effectively remove any matching strings:

 

mceleavey_0-1660642921911.png

 

This gives the following:

 

mceleavey_1-1660642945980.png

 

This will ONLY replace those non-word characters at the beginning and end of the string, and will replace ALL of them, not just one.

 

I hope this helps,

 

M.



Bulien

PhilipMannering
16 - Nebula
16 - Nebula

@binuacs I think you'd want to use the Trim() here to keep the stuff in the middle right? You can still give it the list of punctuation marks to remove.

Trim([Field], '!@#$^&*,/')
JamesBills
8 - Asteroid

Thanks. Yes the second one works for me. Thank you all for quick responses

Labels