Alteryx Designer Desktop Discussions

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

Trim dash space number

tf05
7 - Meteor

I need to trim anything past the - which then contains numbers

 

For example

 

'Town - Building - 125475' = Trimmed to 'Town - Building'

 

Tried a few REGRX_Replace but can only get the numbers to replace leaving the -

 

Thanks

4 REPLIES 4
DataNath
17 - Castor

Try Regex parse with the following:

 

(.*)\s-\s\d+

 

Works for your example but let me know if you run into any issues.

binuacs
20 - Arcturus

@tf05 use the formula REGEX_Replace([String],’(.*)\-.+’,’$1’) 

IraWatt
17 - Castor
17 - Castor

Hey @tf05,

I used 

 

-\s\d+

 

IraWatt_0-1651660585672.png

this matches to the - then a space then 1 or more numbers after.

HTH,

Ira

MarqueeCrew
20 - Arcturus
20 - Arcturus

@tf05 ,

 

Let's take a look at the RegEx question and apply a little explanation.  Your challenge is to use the RegEx_Replace() function and perform a trim (deletion of target data from the source) against your field.

 

We don't know how you intend to use the result, so let's start there.  If you are UPDATING the current field, you'll want to use either a FORMULA tool or the RegEx tool.  I'm partial to the FORMULA tool for beginners.  You also could be FILTERING rows based upon the presence of one of these field values.  In this case you might not care about saving the result.  If you're creating a new field/variable, you'll likely use the FORMULA tool (again you could use RegEx tool).

 

The challenge that you were having when trying this on your own were with the RegEx_Replace function.  Let's start there:

 

$ is the "End of a String".    If you want to assure yourself that the replacement is at the end of the field, you'll likely want one of these.

\s is a "WHITESPACE" character.  It actually matches any space, tab or newline character.  If you only want to match a space, you can simply type a space character.  this makes it a little bit harder to read.

- is a "DASH" character.  It is NOT necessary to place a slash \ in front of it.  When used as a token to indicate a range of characters [0123456789] or [0-9] it does take on a different meaning.

\d is any numeric digit.

+ is a quantifier.  It means that you have 1 or more of whatever token precedes it.

 

So if you write:

 

Regex_Replace([Field1],"spaceDASHspaceOneOrManyDigitsENDofString",'')

 

you will look for the expression (yet to be written in RegEx) with nothing (quote quote).

 

Regex_Replace([Field1],"\s-\s\d+$",'')

 

Here's another formula:

Regex_Replace([Field1],"\b.-.\d+$",'')

You can use https://regex101.com/ to explain what it does.

 

The 2nd expression is more expensive than the first expression.  It does however teach a concept that is very useful to learn.

 

Cheers,

 

Mark

 

Alteryx ACE & Top Community Contributor

Chaos reigns within. Repent, reflect and restart. Order shall return.
Please Subscribe to my youTube channel.
Labels