Match and get distinct

Hi,

I got an IEnumberablle variable but I only want unique items. What to do? I tried

(my regex)(?<!\1[\s\S]\1) but this doesn’t work for me. Thanks

I am looping this regex result and putting all the items in a string but I only need unique values. I tried to use if statement for the assign, but won;t work

claimL = if(claimL.Contains(item.ToString), “”, String.Join(“;”, claimL, item.ToString))

it throws me an error
image

What to do?

Lavina

Hi, I’m not sure what the regex is doing because I’m fairly inexperienced, but I do know that error is because the variable you are using currently has a null value. You are trying to apply the .contains method to something which is nothing.

To initialize it you can either assign it an interim value (ie if its a string, you can assign “”), or if its a collection it will need to be something like ‘New list(of string)’ or whatever the syntax is for your particular variable type.

You are totally right! Got it! Assign default value and works! Thanks a lot

1 Like

I agree with @KEntwistle. claimL is nothing the first time it hits that activity, so you need to either convert it to an empty string with .ToString (claimL.ToString.Contains()) or assign “” to the variable’s Default Value in the variables section.

Additionally, you can do lot of this manipulation using vb.net expression instead of using a For loop.
for example,

claimL = String.Join(",", ienumberablevariable.Select(Function(x) Regex.Match(x, pattern).Value ).Distinct )

.Distinct should only pull the unique values too, unless I have mistaken.

In that example, you would only need one assign activity and no loops.
I went ahead an added that tip for you incase you consider it in the future :smile:

Regards.

2 Likes

Hi @ClaytonM,

This is such an awesome tip :D! thanks a lot - Yes!! I’ve been trying to use more Assign activity like you do but have hard time coming up with the codes. I think using loops maybe slower? not sure… I will try to find some online course and study about vb.net and regex now. Thanks! ^^

Lavina

Hi @lavint
Is your problem for duplication solved??
I am facing same issue and not getting any proper solution, below is the link

I tried using distinct, that doesn’t work.Also i tried to suffix (?<!\1[\s\S]\1) with my regex pattern, it throws error.
Please can you help me out with it.
My original pattern is “([\w]*/[\d]{1,2}-[\d]{1,2}-[\d]{2,3}-[\w]{4}/[\d]{1,2})"
and i want to extract only the distinct ones but it is even extracting the duplicate ones.

Hi,

Yup! I use

After you get the duplicate string in the Matches activity, output as resultMatch

For each item in resultMatch
Assign L = if(L.Contains(item.ToString), L, String.Join(“;”, L, item.ToString))

and L should have only unique items. Also, L needs a default value like “”

Hope it works! Thanks

Thanks A lot, that works.