String replace: any number

Hello,
I’m looking for a way to replace capital letter X between numbers in string like for example ‘XPCBoard100X100mm’ to achieve string like ‘XPCBoard100x100mm’ and not to replace in word XPC. The rule can be that capital letter X is between any numbers, like in this example 0X1. Any ideas?

You can either use python to implement some fuzzy logic or you can just replace both X’s with a first pass and then replace “xPC” with “XPC”.

I’m not fluent in python, so not able to create a script. String can be any, XPC is just an example.

We’ll need more information then. What do these prefixes look like? Are they always 3 characters? Do they always start with X? Is there always a single X in the prefix and no other X’s besides the one in the size?

Without a standard structure or pattern it becomes impossible for us to suggest how to fix the situation.

1 Like

try this maybe?

#Load the Python Standard and DesignScript Libraries
import sys
import clr
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *
import re

#The inputs to this node will be stored as a list in the IN variables.
dataEnteringNode = IN

#Place your code below this line

string = IN[0]

indices = [m.start() for m in re.finditer('X', string)]

for i in indices:
    if string[i-1].isnumeric() and string[i+1].isnumeric():
        string = string[:i] + 'x' + string[i+1:]

#Assign your output to the OUT variable.
OUT = string

String contains any tekst and numbers with X between in Pascal case, numbers can be any, X can occur as prefix a few times and can occur in text also. Some examples of strings: AnyText100x100mm, AnyText9x9mm, AnyText999x88x8mm

I believe my solutions covers those parameters.

@Jagger your solution is good, but it only works on single string not on list:

Assuming that values are always SomewordsWhichMayHaveXInThemNumberXNumber, then you can get the last index of X and replace it with x.

String.LastIndexOf will give you the index of the last X as a start.

1 Like

How rigid are your inputs? If there’s a lot of potential to have variance in syntax, some are width x height, some are width x height x depth, some have ‘mm’ on all the numbers, some only at the end, space have spaces, some don’t, etc, then using regex is quite a strong way to handle situations like this

Obviously not thoroughly tested, just a 1 minute slap together for this response, but it’s an approach you could take.

If your inputs were more consistent, always being either numberXnumber or numberXnumberXnumber then you could use a simpler regex like [\d]+[X][\d]+[X]?

1 Like

Just another approach (based of @jacob.small remark).

This version utilizes more nodes but keeps the strings together.

For really big strings both versions will perform better if they were combined into a function.

another method which doesn’t require import re expression:

input = IN[0]

def replaceX(lst):
	text = ''
	for i, l in enumerate(lst):
		try:
			if l == 'X' and isinstance(int(lst[i-1]), int) and isinstance(int(lst[i+1]), int):
				text += 'x'
			else:
				text += l
		except:
			text += l
	return text

OUT = [replaceX(k) for k in input]
1 Like