Search and replace string contents

This has been puzzling me all afternoon.
I am trying to work out a way of searching a list and finding a code that could have different values in different locations but is always the same format and then replacing it.
The code is always in the format “Letter Number Number” (also it always has a hyphen following)
image
I am going to swap the letter to the back of the code downstream and push it back in, but I don’t know how to look and extract this data then manipulate it. The final result will be “Number Number Letter”.
Thanks for any help.

If it is not in the first 3 characters, will it always be preceded by a hyphen?
It could be a pretty long python if statement. I am sure you could do a node version as well but that would be equally or more complicated.

How I would tackle this:
If statement for the first three characters is equal to alpha, numeric, numeric, hyphen. if not, then parse for - and run the check again. keeps going until either you find it and break or run through it all, then compiles the list of them.

Yes it would, but the defining characteristic would be the alpha numeric numeric code as these would be only occurring once in the string code and no other places in the string.
Is it possible to search the string for alpha numeric numeric? I have never had to do this in the past as I can usually interrogate the list using position…this is a bit of a special case.

Is this what you wanted ?

Code for a single string :

input = IN[0]

for i in range(len(input)-2):
	if (input[i].isalpha()):
		if(input[i+1].isdigit()):
			if(input[i+2].isdigit()):
				letter = input[i]
				digit1 = input[i+1]
				digit2 = input[i+2]
				result = input[:i]+digit1+digit2+letter+input[i+3:]

OUT = result

Code for a list of strings :

input = IN[0]

result = [];

for i in range(len(input)):
	for j in range(len(input[i])-2):
		if (input[i][j].isalpha()):
			if(input[i][j+1].isdigit()):
				if(input[i][j+2].isdigit()):
					letter = input[i][j]
					digit1 = input[i][j+1]
					digit2 = input[i][j+2]
					pre_result = input[i][:j]+digit1+digit2+letter+input[i][j+3:]
	result.append(pre_result);

OUT = result
3 Likes

There is in python, not sure if it is built in Dynamo nodes other than making a full alphabet in a codeblock.

In python: str.isalpha() will give a true or false if contains only alpha.
Similarly, str.isdigit() will work for numbers.

So you would check the first 3 character positions with .isalpha() and .isdigit(). If it fails, you have to find the next hyphen and search again. when true, return all three.

Like this?

b = DSCore.Object.IsNull(String.ToNumber(String.Split(a,"")));
c = List.AllIndicesOf(b<1>,false);
d = String.Substring(a,c,3)==null?"null":String.Substring(a,c,3);
e = List.FilterByBoolMask(c<1>,String.EndsWith(d,"-",true)<1>)["in"];
f = String.Substring(a,e,-2);
g = String.EndsWith(f,"-")==null?true:String.EndsWith(f,"-");
h = List.FilterByBoolMask(e,DSCore.Object.IsNull(f)||g)["in"];
i = String.Remove(String.Insert(a,h+2,String.Substring(a,h,-1)),h-1,1);
j = List.LastItem(List.SetUnion(a<1>,i<1>)<1>);

2 Likes

Thanks for the design script solution. I am getting an error when I put it into the codeblock?
It is also giving me 3 inputs?

1 Like

This Python solution works for the list I have. Thanks for the solution. Really appreciated.
I am just trying to get my head around the Design script solution also.

It seems you have packages using the same names ‘‘list’’ and ‘‘string’’ add ‘‘DSCore.’’ In front of these

You can see there is an issue by inputs being created for the two :slight_smile:

1 Like

Thanks for the clarification.

As suggested by @Jonathan.Olesen seems like you’ll need to prefix “DSCore” to avoid the conflict with other installed packages

b = DSCore.Object.IsNull(DSCore.String.ToNumber(DSCore.String.Split(a,"")));
c = DSCore.List.AllIndicesOf(b<1>,false);
d = DSCore.String.Substring(a,c,3)==null?"null":DSCore.String.Substring(a,c,3);
e = DSCore.List.FilterByBoolMask(c<1>,DSCore.String.EndsWith(d,"-",true)<1>)["in"];
f = DSCore.String.Substring(a,e,-2);
g = DSCore.String.EndsWith(f,"-")==null?true:DSCore.String.EndsWith(f,"-");
h = DSCore.List.FilterByBoolMask(e,DSCore.Object.IsNull(f)||g)["in"];
i = DSCore.String.Remove(DSCore.String.Insert(a,h+2,DSCore.String.Substring(a,h,-1)),h-1,1);
j = DSCore.List.LastItem(DSCore.List.SetUnion(a<1>,i<1>)<1>); 

Meanwhile, here is the node version of the same …


stringReplace.dyn (50.0 KB)

2 Likes

That is great. Thank you for supplying the node version also. Very helpful. Much Appreciated.

1 Like