Start Free Trial

Alteryx Designer Desktop Discussions

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

Regex Replace starting from an n occurence

Nouha
6 - Meteoroid

Hello everyone, 

 

if someone can help figure this problem i'd be greatful

 

my data looks like this 

 

fieldstarting afternbcar
,A,BC,AZRG11
,DB,CQ,AEYG21
,ETR,AC,A3,4AQW22

 

and what i want to do is remove from "Field" the specified number of commas in "nbcar" after the comma specifed in the "starting after"

 

so it would look like this 

fieldstarting afternbcarnew_Field
,A,BC,AZRG11,ABC,AZRG
,DB,CQ,AEYG21,DB,CQAEYG
,ETR,AC,A3,4A,QW,TRZ22,ETR,ACA34A,QW,TRZ

 

I thought maybe it can be done with Regex_replace but i can't seem to make it work

 

Thank you all, 

Nouha

3 REPLIES 3
CharlieS
17 - Castor
17 - Castor

There's probably a more succinct way to do this with RegEx, but here's a solution without RegEx. I split each field into rows and used the [starting after] and [nbcar] fields to determine how to concatenate the rows back together (with or without commas). A Tile tool provided a grouped record ID field. 

 

WillBowman
9 - Comet
((,[^,]*){2}),(.*)
 
Basically replace 2 with the amount of commas before the one you want to replace and then concatenate groups 1 and 3. In alteryx use the regex parse tool and I think you can concatenate there.
 
In python or R just +
 
Here's the output i got:
 
Full match 0-20 `,ETR,AC,A3,4A,QW,TRZ`
Group 1. 0-7 `,ETR,AC`
Group 2. 4-7 `,AC`
Group 3. 8-20 `A3,4A,QW,TRZ`
 
 

To account for the NBCHAR you need to capture subsequent groups and then run a separate replace (in scripting we could just use replace but this is alreryx). For this you want

((,[^,]*){2})((,[^,]*){2})(.*)

And to run replace “,” on group 3. Then concat groups 1,3,5
ponraj
13 - Pulsar

Here is the sample workflow for your case.  Hope this is helpful. workflowworkflow

 

InputInputResultsResults

Labels
Top Solution Authors