Community Spring Cleaning week is here! Join your fellow Maveryx in digging through your old posts and marking comments on them as solved. Learn more here!

Alteryx Designer Desktop Discussions

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

Pass Variable into number of occurrences in Regex

gwiz
8 - Asteroid

Is it possible to pass a variable into the quantity designation in regex, the curly brackets, such that the amount of back slashes it looks for depends on how far to move back in the field (in this case a file path).

 

Essentially it should look back X number of back slashes (in a filepath) depending on the amount to move back, field [MoveBack]. [MoveBack] is a number type (integer/byte).

 

REGEX_Replace([Field1],"(.*)(\\..*){[MoveBack]}", "$1") + "\" + [Index] +"_" +[Title]

 

I try to pass the variable through but it does not work for me. Any tips would be helpful.

 

Thanks!

 

 

7 REPLIES 7
rfoster7
9 - Comet

REGEX_Replace([Field1],"(.*)(\\..*){" + [Moveback] + "}", "$1") + "\" + [Index] +"_" +[Title]

 

you have to offset your [Moveback] with +s to add it to the string of the regexp pattern

BrandonB
Alteryx
Alteryx

You could build the regex formula using a formula tool to create it as a string and then wrap the regex tool in a macro that takes the regex formula in as a control parameter.

IraWatt
17 - Castor
17 - Castor

Hey @gwiz,

This is definitely achievable in the formula tool:

IraWatt_0-1652396437428.png

 

Make sure all your datatypes are strings or just convert them with the toString function

 

gwiz
8 - Asteroid

that didn't seem to work :/ can you clarify?

gwiz_0-1652397015987.png

 

rfoster7
9 - Comet

sure. The regexp pattern is just a string like any other string. So you can build it out using string concatenation.

 

I think your MoveBack, you said, was byte/integer right? so to append that into the string, it needs to be a string. So 

 

REGEX_Replace([Field1],"(.*)(\\..*){" + tostring([Moveback]) + "}", "$1") + "\" + [Index] +"_" +[Title]

 

-SHOULD- do it. thanks to IraWatt for pointing that out. I didn't think about it. 

gwiz
8 - Asteroid

@rfoster7 and @IraWatt thanks! I did not realize that [MoveBack] or the variable you pass has to be a string. but makes sense. Thanks for the help everyone.

IraWatt
17 - Castor
17 - Castor

No worries @gwiz !

Labels