Find using Regular expressions

3.44K viewsScripting
0

The documentation for e.g. [font=”Courier New”]matrices.find(param)[/font] says that the parameter can be a regular expression – very convenient if you’re looking to do a partial match on a name. However I can’t make this work.

For example
[code:1gylbusm]matrices.each{if (it.name =~ /foo/ ) x << it }[/code:1gylbusm] will return a matrix with a name that includes “foo”,
but
[code:1gylbusm]x = matrices.find(/foo/)[/code:1gylbusm] doesn’t return anything

0

Simon,

Your point is taken, but I think we’re going to leave it as is for now. However, some better documentation is probably in order and is something that I will see to. Thanks for bringing it to our attention.

Ben

0

Ben,

It seems the issue is that the matching is done on the full string. So to match a matrix named “foo bar” you need to use: [font=”Courier New”] matrices.find(/foo.*/)[/font]

This is equivalent to the Groovy operator [font=”Courier New”] ==~ [/font]

I would suggest it would be more natural to use the equivalent of [font=”Courier New”] =~ [/font] which finds matches [b:3hz6esdy]within [/b:3hz6esdy]a string. That would enable [font=”Courier New”] matrices.find(/foo/)[/font] to return the matrix “foobar”, not only the matrix “foo”.

But at least I’ve found something that works for now…

Thanks

Simon

0

Hi Simon,

It appears to work for me. I’ve attached a model that runs okay on my machine, and demonstrates the feature. Let me know if it works for you.

Ben

Attachments

regex.model