Useful string expressions (mel) 05 Sep 08
Collection of useful string expressions for mel
The nice name of an object from the ‘long’ path
string $matchString = "|locator1|null1|pCube1";
match("[^|]*$", $matchString);
// Result: pCube1 //
The parent of an object from the ‘long’ path
string $matchString = "|locator1|null1|pCube1";
substitute("|[^|]*$", $matchString, "");
// Result: |locator1|null1 //
Match the specified attribute name of an object
string $matchString = "|locator1|null1|pCube1.translate";
match("[^.]*$", $matchString);
// Result: translate //
string $matchString = "|locator1|null1|pCube1.vtx[0]";
match("[^.]*$", $matchString);
// Result: vtx[0] //
Match the nice name of an object excluding any includeded attributes
string $matchString = "|locator1|null1|pCube1.translate";
match("[^|]*$", match("^[^.]*", $matchString));
// Result: pCube1 //
Match the attribute excluding any array index information
string $matchString = "|locator1|null1|pCube1.vtx[0]";
match("[^.]*$", match("^[^\[]*", $matchString));
// Result: vtx //
Remove trailing character (i.e Whitespace, new line, tab etc)
string $matchString = "text string \n\r\t";
substitute("[( \n\t\r)]*$", $matchString, "");
// Result: text string //
Remove leading character (i.e Whitespace, new line, tab etc)
string $matchString = " \n\r\ttext string";
substitute("^[( \n\t\r)]*", $matchString, "");
// Result: text string //
Drive letter from path
string $matchString = "c:\\testdir\\subdirectoryA/subdirectoryB/yourFile.txt";
match("^[^:]*", $matchString);
// Result: c //
Full directory from path
string $matchString = "c:\\testdir\\subdirectoryA/subdirectoryB/yourFile.txt";
substitute("[^(\\/)]*$", $matchString, "");
// Result: c:\testdir\subdirectoryA/subdirectoryB/ //
Full filename from path
string $matchString = "c:\\testdir\\subdirectoryA/subdirectoryB/yourFile.txt";
match("[^(\\/)]*$", $matchString);
// Result: yourFile.txt //
File extension from path
string $matchString = "c:\\testdir\\subdirectoryA/subdirectoryB/yourFile.txt";
match("[^(.)]*$", $matchString);
// Result: txt //
Filename excluding extension from path
string $matchString = "c:\\testdir\\subdirectoryA/subdirectoryB/yourFile.txt";
match("^[^.]*", match("[^(\\/)]*$", $matchString));;
// Result: yourFile //
Backslashes to forward slashes
string $matchString = "c:\\testdir\\subdirectoryA/subdirectoryB/yourFile.txt"; substituteAllString($matchString, "\\", "/"); // Result: c:/testdir/subdirectoryA/subdirectoryB/yourFile.txt //

August 9th, 2009 at 10:50 am
Thanks for all your Mel stuff.
Very useful.