Regular Expressions (XSISDK)

Regular Expressions are a powerful way to search and manipulate strings.

All XSI scripting languages directly support regular expressions.

The challenge with regular expressions is that the syntax is pretty hard to learn and maintain, but the powerful results can outweigh the initial learning curve.

Replacing Numbers with Strings

An real-life usage of regular expressions is to rename XSI objects, perhaps to follow a standard naming scheme. An example from the xsi mailing list was to try to replace objects named like Foo1, Foo2, Foo4 with Foo_A, Foo_B, Foo_D.

This is a solution, based on Helge Mathee's suggestion (which modifies the objects in the selection):

var r=new RegExp(/(\d*)$/);
for(var i=0;i<selection.count;i++)
  if((s=selection(i))&&(m=r.exec(s.name)))
    s.name=s.name.replace(r,"_" + String.fromCharCode(65+eval(m[1])));


This page was last modified 07:58, 31 Aug 2007.
This page has been accessed 5542 times.

© Copyright 2009 Autodesk Inc. All Rights Reserved. Privacy Policy | Legal Notices and Trademarks | Report Piracy