Regular expressions in Dynamo

I’m trying to break apart a string in Dynamo. I think the easiest solution is regular expressions. Can you use regular expressions in a code block?

1 Like

Hi Danny, no, there’s only a StringReplace node which works fine for simple cases of search/replace, but not for any rule-based stuff. You can however use Python for working with RegExp. About a year ago, I made a Python-based custom node to match regular expressions in a string. I have now added a couple more such nodes (replace regexp, split by regexp, …) to package Clockwork. Enjoy.

1 Like

Clockwork has String.ReplaceRegularExpression . Example:

StringReplaceRegularExpression

This is great! thank you I’ve downloaded it.

I downloaded the Clockwork package. I couldn’t replicate the example you had. I looked for a document or an email from anyone who might have worked on the Clockwork regular expression without any luck. Do you know what I need to set this up or what I might be doing wrong? The str return gives me a empty string.

I was able to replicate it based off of yours Danny. I uploaded mine that works see if you are still having the issue. regular expression

Hi Danny,

Try this,

1 Like

It looks identical. I’ve found that often I have to unplug a node, run, re-plug, run again to get Dynamo to work.

I read that it’s some kind of some caching buglet.

Here is the slimmest module I could come up with:

import re			##Import for re or Regular Expressions
###INPUTS (Two Inputs- 0 and 1- ad inputs to python node to correspond with th [+] button)
StrList=IN[0]		##Simple list of items to match
regexExp=IN[1]		##Regexp string to match 
					##see https://docs.python.org/3.3/howto/regex.html "Regular expressions in dynamo" and 
					##https://regex101.com for regular expressions 101 testing
##OUTPUT
Outlist = []		##Outlist TRUE if match is find for each item in list
###Initialize Regexp
Regex = re.compile(regexExp, re.IGNORECASE)

###The actual RegExp compare for each item in the list
for item in StrList: ## For each item in the list run a match 
	Outlist.append ( Regex.match(item) is not None)	##If match is not NONE then it is a match (true) - else (false)- append that to the list for each item

OUT=Outlist		##Set output to results

I appreciate all the coded modules but sometimes I just want the how-to nuggets…

4 Likes