# Use Feature Constructor in Orange to extract number from string?

I would like to use the Feature Constructor to extract numbers from a column of strings.

 Input          Output
S.c 3224   ---> 3224
South      ---> 0
322523     ---> 322523
S-1c 4     ---> 14
-1.37      ---> 137


Internal Orange 3.7.0 Documentation says For continuous variables you only have to construct an expression in Python. Great!

I found information about allowed functions in the feature constructor code:

#Only expressions with no list,set,dict,generator comprehensions
#are accepted.
<...>
__ALLOWED = [
"Ellipsis", "False", "None", "True", "abs", "all", "any", "acsii",
"bin", "bool", "bytearray", "bytes", "chr", "complex", "dict",
"divmod", "enumerate", "filter", "float", "format", "frozenset",
"getattr", "hasattr", "hash", "hex", "id", "int", "iter", "len",
"list", "map", "memoryview", "next", "object",
"oct", "ord", "pow", "range", "repr", "reversed", "round",
"set", "slice", "sorted", "str", "tuple", "type",
"zip"
]


Based on the list, I was hoping to use an expression like this:
int(filter(str.isdigit, inputStringColumn) or 0)

...but that is an "Invalid Expression" (no further feedback)

How can I extract a number from a string?

• How about int(inputStringColumn) if inputStringColumn.isdigit() else 0 ? – Stephen Rauch Nov 15 '17 at 20:40
• @StephenRauch "Attribute Error: 'Value' object has no attribute 'isdigit'" – Adam Nov 15 '17 at 20:45
• @StephenRauch Part of the trouble - I thought the column was a string because it has letters, but it isn't. If I first cast to string, then your solution at least runs . int(str(inputStringColumn)) if str(inputStringColumn).isdigit() else 0 It doesn't work completely, however - when the whole field is digits, it returns those digits. Otherwise it returns 0 (it isn't going piecewise through.) – Adam Nov 15 '17 at 20:49
• With help from the error message generated by Stephen Rauch's suggestion, I worked out one way to do this: int( ''.join(filter(str.isdigit, "0"+str(inputColumn) ) )) I will write this up as an answer later, unless someone comes along with something better. – Adam Nov 15 '17 at 21:00
• It would be much nicer if re (regex) module were exposed. While writing up the answer, also consider opening a bug/enhancement report. :D – K3---rnc Nov 16 '17 at 12:48

int(''.join(filter(str.isdigit, "0"+str(inputColumn))))