Filtering by character types in string

Hi,

I’m trying to find out if the first few characters match some rules.

0 is a letter
1 is a number
2 is a .
3 is a letter

Currently I’m doing this like below but I was wondering if there is a more efficient way?
image
image
image

Not sure if it’s more efficient, but it might be a bit cleaner. Does require performing 63 equality tests for each string you feed it, but 63*47 isn’t going to break the bank from a run speed standpoint. I certainly wouldn’t do this for more then single character tests as shown here, but I found that it worked in 23 milliseconds for 220 4 digit strings. Results may vary, and I may be missing a case or two, but I don’t have a real data set to test it on at the moment.

1 Like

You could also do something like this to check for each character by index.

1 Like

Regex to the rescue:

Python:

# Import RE (Regex) module
import re

# The inputs to this node will be stored as a list in the IN variables.
data = IN[0]	# Input strings

# Alpha, numeric, '.', alpha
check = "[A-Z][0-9][\.][A-Z]"	# RE Search string

OUT = []
for d in data:
	if re.search(check, d):
		OUT.append(True)
	else:
		OUT.append(False)

I haven’t used the regex node you are using but you could try using this as the regex input:

[A-Z][0-9][\.][A-Z] as a string

2 Likes