Grasshopper RegEx Components

Continuing the discussion from Extract Text from Data List:

There’s an early prerelease Grasshopper Regex plugin called Greg on the Package Manager for Rhino 8 and up. It implements Search, Replace, FindAll and Split.

There’s a gh file with examples included in the shared folder under the installed package. Note that this is found with the Rhino plugins (%USERPROFILE%\AppData\Roaming\McNeel\Rhinoceros\packages\8.0\Greg\0.1.9592-beta+32054\shared) and not in the Grasshopper components folder - a wrinkle of the build process yet to be investigated…

Feedback welcomed.

:heart:

Thank you for this!

The reason that I suggested domains is because I thought it would simplify the number of outputs. Using Deconstruct Domain you could get the start and end indices, by pushing the domains through an Integer container, get the fragment lengths, and by using Null item get the match booleans. But I suppose that is personal preference.

I honestly do not know how to use regular expressions. I just know what they are supposed to do and that they are quite powerful. Learning all the rules is tedious. Luckily this is the age of AI, and an AI does a decent job of creating a regular expression from a sentence explanation of what you want and will explain to you how the regular expression is composed.

I am wondering (because I don’t know) is the difference in inserting / replacing / splitting not things that the regular expression itself does? Different components for these functions are required?

Let me give that one some thought…

Bear in mind that there are different flavours of regex out there and they might give unobvious variations. The regex components here use the Python / Perl syntax so it might be wise to include that in the specification. Even then, I have found that Grasshopper Python doesn’t support the latest lookahead and lookbehind syntax (but does support the older syntax) so having some understanding might help with head scratching AI failures! This is an excellent resource: Regular-Expressions.info - Regex Tutorial, Examples and Reference - Regexp Patterns.

Well, there’s the regular expression pattern and there’s the regular expression tool that uses the pattern. The four components here mirror Python, which implements quite a lot of separate functions, these being the four main ones. I don’t know whether the Python functionality is split for performance reasons or simply because it evolved over time and they didn’t want to change the earliest function for fear of breaking applications. But my philosophy is that those folk are a lot smarter than me so I’ll do what they do… (Which has the benefit of keeping the code really simple so I’m less likely to cock it up.)

Regards
Jeremy

So the components mirror the implementation of RegEx in the Python/Perl environment? That makes more sense.

The way I understood it, RegEx is simply a pattern matching, so I was wondering why the pattern matching was repeatedly integrated into each of the search / find / replace functions. More in line with the visual programming model would be if the pattern matching was its own component, the results of which would be used by the inputs of insert, remove, replace, and split components. But perhaps there is something I am missing here which necessitates rolling the pattern matching into each of the search / find / replace functions.

Either way, it is a big win to a have a RegEx component that does more than give a simple boolean when a match is made.

Why “Greg”?

Grasshopper regex