Filtering integers in a string

Hi all,

Tnx to @Tomasz_Puchala I was able to filter integers in a string.
But now, when the input is a list of strings, I am getting an error that the input is a list. Can sombody tell me what i am doing wrong?

Previous post: Index of integer in a string

tnx in advance

It is always best to write the code for the worst case scenario and check if the input meets that case.
In this situation, it would work by creating a list if the input is not one already:

import re

input = IN[0]

#If the input is not a list, it will create one.
if not hasattr(input, '__iter__'):
	input = [input]
OUT = [re.sub('\d+', ' ', string) for string in input]

Hope it helps.


Wow, it worked. Thank u. I am still trying to learn Pyhon in Dynamo.
If you have some time, could you break down the code and explain how it works?

Tnx again.


Hello Rasa,

The following should explain what is going on with this Python snippet.

We first import the re (RegularExpression) module. This is one of only a couple that are allowed to be imported without pointing to the particular .dll file.

import re

We then create a named variable for our input port, at the toggled button of ‘IN[0]’. We do not need to unwrap if the inputs unless they are Revit Elements.

input = IN[0]

We want to check if the input is a single item, or a list. To do so, we can use the internal Python commands of ‘hassattr’ (Has Attribute) and the call of ‘_ iter _’, which is simply a true or false statement on whether or not the element in question (i.e our List) is iterable (Which means taking each item of something, one after another to subsequently do something to). It will then ask if it’s NOT iterable (Using logical operators of ‘if’ and ‘not’). This means, simply put, that if the input list is iterable - it’s a list, and if it’s not, it’s a single item. In the case of it being a single item, we simply want to put the item into a list (Of only itself) by calling a new return value for ‘input’. In python, to create a list, we use square brackets [ ] and here we wrap our input with them in the case of a single item.

if not hasattr(input, '__iter__'):
	input = [input]

We then want run a list comprehension over the OUT port. Our comprehension uses the re module, and calls the ‘sub’ method - which allows us to replace sub-string elements. In this case, the information supplied to the RegularExpression is taking all instances of numbers and replacing them with empty spaces.

Our regular expression search key uses \d which stipulates match any data that is a number followed by a + key that means the regular expression will match one or more repetitions of the proceeding RE. It calls that for every single item in the input list, demarcate as ‘string’.

A list comprehension allows us to neatly wrap up loops into a single line by calling the following: [ item.doingSomething for item in inputList ], where item is our variable and doingSomething is our check - in this case our Regular Expression and it auto outputs a list (Meaning you don’t have to define an empty catchment list earlier).

Our list comprehension is as follows: [ callREModuleEnttitledSub(ReplaceAllNumbersWithAnEmptyStringOnVariableCalledString) for everySingleItem in ourList ]

OUT = [re.sub('\d+', ' ', string) for string in input]

Has Attribute function
__ iter __ property
RegularExpression Python Module


(clockwork package)

this is my small regEx docu, but some parts are in german, maybe it helps:

you can also use to test your regEx string, but u should pick on the left the python flavor


@solamour, thank you for a good explanation. I dont understand it fully, but i have something to study and trying to understand it. Once again txn

@Thomas_Vogt Tnx will look into this

It does take some time to wrap your head around - hence the embedding of links! :slight_smile: Best of luck with your Python journey - it’s a whole lot of fun and opens many doors.

1 Like